所以我是一个初学者,我正在尝试解决这个问题:
这是我的代码片段:
int main()
{
list<Shape*> shapes;
shapes.push_back(new Pentagon(Vertex(20, 10), 8));
shapes.push_back(new Rhombus(Vertex(50, 10), 8));
list<Shape*>::iterator itr = shapes.begin();
while (itr != shapes.end())
{
(*itr)->drawShape();
c.gotoXY(20, 10);
cout << (*itr)->area();
// scale shape (double it)
// draw shape
// rotate shape by 20 degrees
// draw shape
itr++;
}
cout << endl << endl << endl << endl << endl << endl << endl << endl;
system("pause");
}因此,基本上,第一个文本块是添加两个新对象,五边形和菱形到列表形状。
这些对象有3个值,x和y坐标以及大小。
然后在迭代器中,我试图在控制台中绘制这些形状,它工作得很好,但我在c.gotoXY();函数上遇到了问题。
基本上,我希望该区域显示在形状的中间。当我手动输入坐标时,它工作得很好:代码中的(20, 10),用于五角形。您可以看到,它与我在列表中添加新内容时输入的值完全相同。
但我会有很多不同的形状,我不能只是手动输入值。
我想将x和y的值链接到我的形状列表中对象的值,如下所示:
http://puu.sh/hlMMR/2f536907f9.png
所以基本上,每次迭代器遍历时,它都会获取列表中下一个对象的x和y值,并将它们粘贴到c.gotoXY();中
我希望我已经解释得足够清楚了,因为我自己几乎不理解它。
请帮帮忙。
@编辑~
当我尝试使用
c.gotoXY(Vertex.getX(), Vertex.getY);我得到这个: error C3867:'Vertex::getY':函数调用缺少参数列表;使用'&Vertex::getY‘创建指向成员的指针
发布于 2015-04-21 23:32:59
我假设您的Shape类型有一个成员函数,如下所示:
Vertex getPosition() const;并且您的Vertex类型具有类似以下内容的成员函数:
int getX() const;
int getY() const;在这种情况下,您可能会编写如下代码:
Shape* curr=*itr; //Get a pointer to the current shape from the iterator.
Vertex pos=curr->getPosition());//Get the position of the current shape.
c.gotoXY(pos.getX(),pos.getY());//Goto the current position of the current shape.代替:
c.gotoXY(20, 10);如果您还没有提供访问形状顶点和顶点坐标的方法,这是要解决的第一个问题。
编辑:我在评论中注意到getX()和getY()已经就位。它可以访问仍然打开的形状中的Vertex。
https://stackoverflow.com/questions/29776532
复制相似问题