Typedef 向量给了我错误
Typedef vector gives me errors
>我目前正在尝试从 ShapeTwoD 类中的驱动程序类访问向量。
这是驱动程序类的头文件:
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;
}
这是错误消息:
驱动程序.cpp:109:错误:"." 标记之前应存在非限定 ID
我正在尝试做的是从 Driver 类访问向量,其中向量已经填充了 ShapeTwoD 类中的用户输入数据以进行计算。
我做错了什么?
编辑
我在 ShapeTwoD 标头中做了这样的事情:
typedef ShapeTwoD* Shape2D;
Shape2D sd;
typedef vector<ShapeTwoD*> Shapes;
Shapes shapes;
驱动程序标头中类似这样的东西:
typedef ShapeTwoD* Shape2D;
Shape2D sd;
typedef vector<ShapeTwoD*> Shapes;
Shapes shapes;
现在我收到一个错误,在驱动程序.cpp文件中显示sd not declared in this scope
.我是否使用typedef
正确创建了对象?还是我用错了typedef
?
typedef vector<ShapeTwoD*> shapes;
shapes.push_back(sd);
第一行表示shapes
是类型的名称。第二行(发生错误的位置)尝试使用 shapes
作为对象的名称。
类型名称形状在类驱动程序中定义。所以在类之外,你必须写限定名 Driver::shapes。此外,形状不是对象。您不能写例如shapes.size()。
相关文章:
- C++中函数的向量返回类型引发错误
- 尝试将unique_ptrs推送到向量时使用纯虚拟函数错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 如何使用C++初始化向量;脚本化值不是数组、指针或矢量错误
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 使用全局声明的向量时,C++双重释放错误/损坏
- 代码使用向量成功运行,但使用数组显示错误
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- 全局向量导致 C++ 程序结束时出现段错误
- 为什么我在向量初始化时收到此错误?
- 在C++中比较来自向量的字符串时出现分段错误
- 将图形表示为unordered_map<字符串、向量>时拓扑排序错误<string>
- 为什么在向量上使用 std::find() 时会出现错误?
- 分割错误:向量中的擦除功能
- 编译错误向量排序和联合
- SIGBART 错误向量<列表<myClass>>
- 编译错误 ::向量 ::迭代器
- 为什么 std::copy 抛出错误向量迭代器 + 偏移量超出范围并且无法复制
- C++STL set_union获取错误向量迭代器+偏移量超出范围
- C++错误向量无法正确输出