【发布时间】:2015-05-22 16:10:33
【问题描述】:
我遇到了一个问题,我无法打印保存在列表“顶点”顶点中的坐标集。为了解释,“顶点”是一个基本上包含两个坐标的类,这些坐标构成一个顶点(其中两条线形状满足)。问题出在第二个 for 循环中,该循环旨在打印出列表中保存的坐标。任何人都可以帮助我尝试将其打印出来吗?我尝试了很多方法,到目前为止,错误一直给我带来问题.. 代码如下,感谢帮助!
主要内容
int main(){
//obj
Console con;
RandomNumber rand;
Vertex vertex;
//Declarations
list<Vertex>vertices;
//list<Vertex>::iterator ii;
//set Vertex coords and push into list
for (int i = 0; i <= 8; i++){
vertices.push_back(Vertex(rand.random(0, 10), rand.random(0, 10)));
}
//iterate through the list outputting a char at each vertex (coords)
for (list<Vertex>::iterator ii = vertices.begin(); ii != vertices.end(); ii++){
cout << vertices[ii];
}
system("pause");
}
顶点类
#pragma once
class Vertex
{
int x;
int y;
public:
Vertex(int x = 10, int y = 10);
~Vertex();
int getX() const;
int getY() const;
void setX(unsigned x);
void setY(unsigned y);
bool operator== (const Vertex &p2) const;
};
#include "Vertex.h"
Vertex::Vertex(int x, int y)
{
this->x = x;
this->y = y;
}
Vertex::~Vertex(){}
int Vertex::getX() const {
return x;
}
int Vertex::getY() const {
return y;
}
void Vertex::setX(unsigned x) {
this->x = x;
}
void Vertex::setY(unsigned y) {
this->y = y;
}
bool Vertex::operator== (const Vertex &point) const {
return (this->x == point.getX()) && (this->y == point.getY());
}
【问题讨论】:
-
什么是控制台Con?
标签: c++ for-loop iterator variable-assignment