Just started programming in C++.
I’ve created a Point class, a std::list and an iterator like so:
class Point { public: int x, y; Point(int x1, int y1) { x = x1; y = y1; } }; std::list <Point> pointList; std::list <Point>::iterator iter;
I then push new points onto pointList.
Now, I’m needing to iterate through all the points in pointList, so I need to loop using the iterator. This is where I get screwed up.
for(iter = pointList.begin(); iter != pointList.end(); iter++) { Point currentPoint = *iter; glVertex2i(currentPoint.x, currentPoint.y); }
Update
You guys were right, the problem isn’t in my iterating the list. It appears the problem is when I am attempting to push something on to the list.
Exact error:
mouse.cpp: In function
void mouseHandler(int, int, int, int)': mouse.cpp:59: error: conversion fromPoint*’ to non-scalar type `Point’ requested
Those lines are:
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { Point currentPoint = new Point(x, y); pointList.push_front(currentPoint); }
What does it conversion between Point* to non-scalar type Point? I’m just trying to create new points and push them onto the list here.
That should be a valid bit of code.
Using this code:
Although I wouldn’t create a temporary copy of the Point as your code does. I’d rewrite the loop like this:
An iterator is syntactically similar to a pointer.
EDIT: Given your new problem, drop the ‘new’ from the construction line. That’s creating a pointer to a Point, as opposed to a Point on the stack. This would be valid:
Or this:
And you’d be better off with the latter.