Typedef向量给我错误

时间:2013-11-04 18:59:13

标签: c++ vector typedef

我目前正在尝试从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
};

这是错误来自Driver类的方法:

    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

2 个答案:

答案 0 :(得分:3)

typedef vector<ShapeTwoD*> shapes;
shapes.push_back(sd);

第一行表示shapes是类型的名称。第二行(发生错误的地方)尝试使用shapes作为对象的名称。

答案 1 :(得分:0)

类型名称形状在类Driver中定义。所以在课外,你必须写限定名称Driver :: shapes。 此外,形状不是一个对象。你可能不会写例如shapes.size()。

相关问题