Typedef 向量给了我错误

Typedef vector gives me errors

本文关键字:错误 向量 Typedef      更新时间:2023-10-16

>我目前正在尝试从 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()。