【发布时间】:2010-10-06 19:40:45
【问题描述】:
刚开始用 C++ 编程。
我已经创建了一个 Point 类、一个 std::list 和一个迭代器,如下所示:
class Point {
public:
int x, y;
Point(int x1, int y1)
{
x = x1;
y = y1;
}
};
std::list <Point> pointList;
std::list <Point>::iterator iter;
然后我将新点推送到 pointList 上。
现在,我需要遍历 pointList 中的所有点,所以我需要使用迭代器进行循环。这就是我搞砸的地方。
for(iter = pointList.begin(); iter != pointList.end(); iter++)
{
Point currentPoint = *iter;
glVertex2i(currentPoint.x, currentPoint.y);
}
更新
你们是对的,问题不在于我迭代列表。看来问题出在我试图将某些内容推送到列表时。
确切的错误:
mouse.cpp:在函数
void mouseHandler(int, int, int, int)': mouse.cpp:59: error: conversion fromPoint*' 中请求非标量类型 `Point'
这些行是:
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
Point currentPoint = new Point(x, y);
pointList.push_front(currentPoint);
}
Point* 到非标量类型 Point 之间的转换是什么?我只是想创建新点并将它们推到这里的列表中。
【问题讨论】:
-
您没有在示例代码中通过 iter 访问 x 和 y。你得到的确切错误是什么?
-
@codelogic,是的,我是。 currentPoint = *iter;
-
对,所以你不会得到关于通过 iter 访问 x 和 y 的错误(因为你不是,你将它分配给 currentPoint)。请提供确切的编译器错误。
-
@KingNestor 您正在通过 currentPoint 而不是 iter 访问 x 和 y。您正在通过 currentPoint = *iter 复制 iter 所引用的内容。这应该可以编译。
标签: c++ linked-list loops