我目前正在尝试从 ShapeTwoD 类中的 Driver 类访问向量。
这是 Driver 类的头文件:
class Driver {
private:
public:
//ShapeTwoD* sd;
typedef vector<ShapeTwoD*> shapes;
Driver();
friend class ShapeTwoD;
void inputStatisticalData();
void computeArea();
};
这是 ShapeTwoD 类的头文件:
class ShapeTwoD {
private:
string name;
bool containsWarpSpace;
vector<Vertices> vertices;
double area;
public:
ShapeTwoD();
ShapeTwoD(string,bool,vector<Vertices>,double);
ShapeTwoD* sd;
typedef vector<ShapeTwoD*> shapes;
...//other methods
};
这是驱动程序类中错误来自的方法:
if (shape == "square") {
for(int i = 0; i < 4; i++) {
cout << "Please enter x-coordinate of pt." << i+1 << " : ";
cin >> point.x;
cout << "Please enter y-coordinate of pt." << i+1 << " : ";
cin >> point.y;
vertices.push_back(point);
}
sq->setName(shape);
sq->setContainsWarpSpace(type);
sq->setVertices(vertices);
shapes.push_back(sd); //this is the line that gives the error
}
这是我访问向量进行计算的方法:
double ShapeTwoD::computeArea() {
for (vector<ShapeTwoD*>::iterator itr = shapes.begin(); itr != shapes.end(); ++itr) {
if((*itr)->getArea() <= 1) {
(*itr)->setArea((*itr)->computeArea());
cout << "Area : " << (*itr)->getArea() << endl;
}
}
cout << "Computation complete! (" << shapes.size() << " records were updated!" << endl;
}
这是错误消息:
Driver.cpp:109: 错误: '.' 之前的预期 unqualified-id 令牌
我要做的是从 Driver 类访问向量,其中向量已经填充了 ShapeTwoD 类中的用户输入数据以进行计算。
我做错了什么?
编辑
我在 ShapeTwoD 标题中做了类似的事情:
typedef ShapeTwoD* Shape2D;
Shape2D sd;
typedef vector<ShapeTwoD*> Shapes;
Shapes shapes;
在 Driver 标头中是这样的:
typedef ShapeTwoD* Shape2D;
Shape2D sd;
typedef vector<ShapeTwoD*> Shapes;
Shapes shapes;
现在我在 Driver.cpp 文件中收到一个错误,上面写着sd not declared in this scope
。我是否使用 正确创建了对象typedef
?还是我用typedef
错了?