![](/img/trans.png)
[英]How do i program my current program to input a command into another console in C++?
[英]How to point my program to the values I input into the list? C++
所以我是一个初学者,我试图找出这个问题:
这是我的代码:
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();
出现了问题c.gotoXY();
功能。
基本上,我希望该区域显示在形状的中间。 :当我在手动坐标键入它工作得很好(20, 10)
中的代码,为五角大楼。 您会看到它与我向列表中添加新内容时输入的值完全相同。
但是,我将拥有许多不同的形状,而不仅仅是手动输入值。
我想将x和y的值链接到形状列表中的对象的值,如下所示:
http://puu.sh/hlMMR/2f536907f9.png
因此,基本上,每次迭代器遍历时,它将采用列表中下一个对象的x和y值并将其粘贴在c.gotoXY();
我希望我能解释得足够清楚,因为我自己几乎看不懂它。
请帮忙。
@ edit ~~~~~~~~~~
当我尝试使用
c.gotoXY(Vertex.getX(), Vertex.getY);
我得到这个错误C3867:'Vertex :: getY':函数调用缺少参数列表; 使用'&Vertex :: getY'创建一个指向成员的指针
我想象您的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
。
如果Shape
类还没有此类,则合并一个getPosition()
函数,该函数将返回带有初始化对象位置的Vertex
对象。 就像是:
Vertex Shape::getPosition() const
{
return m_Position; // position of the shape (this member would have been set by
// the constructor when you called, for example,
// shapes.push_back(new Pentagon(Vertex(20, 10), 8)); in the
// main function
}
因此,在您的main函数中,您可以调用它来获取位置,并相应地定位形状:
while (itr != shapes.end())
{
(*itr)->drawShape();
Vertex position = (*itr)->getVertex();
c.gotoxy(position.getX(), position.getY());
cout << (*itr)->area();
++itr;
}
另外,在您对原始帖子的修改中:
c.gotoXY(Vertex.getX(), Vertex.getY);
您错过了“()”,因此可能是:
c.gotoXY(Vertex.getX(), Vertex.getY());
但是,这是行不通的,因为您是通过类类型而不是通过对象来调用成员函数getX()
和getY()
的。 您需要在调用类的成员函数之前声明和初始化一个类的对象,就像我在上述while循环中对对象position
所做的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.