简体   繁体   中英

Typedef vector gives me errors

I'm currently trying to access a vector from Driver class in ShapeTwoD class.

This is the header file for Driver class:

class Driver {
private:
public:
//ShapeTwoD* sd;
typedef vector<ShapeTwoD*> shapes;

Driver();
friend class ShapeTwoD;
void inputStatisticalData();
void computeArea();
};

This is the header file for ShapeTwoD class:

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
};

This is the method where the error comes from in Driver class:

    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
}

This is the method where I'm accessing the vector for computations:

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;
}

This is the error msg:

Driver.cpp:109: error: expected unqualified-id before '.' token

What I'm trying to do is to access the vector from Driver class where the vector has already been filled with user input data in ShapeTwoD class to do computation.

What did I do wrong?

EDIT
I have done something like this in ShapeTwoD header :

typedef ShapeTwoD* Shape2D;
Shape2D sd;
typedef vector<ShapeTwoD*> Shapes;
Shapes shapes;

And something like this in Driver header :

typedef ShapeTwoD* Shape2D;
Shape2D sd;
typedef vector<ShapeTwoD*> Shapes;
Shapes shapes;

Now i get an error that in Driver.cpp file that says sd not declared in this scope . Am i creating the object correctly with the typedef ? Or am i using typedef wrongly?

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

The first line says that shapes is the name of a type. The second line (where the error occurs) tries to use shapes as the name of an object.

Type name shapes is defined in class Driver. So outside the class you have to write qualified name Driver::shapes. Moreover shapes is not an object. You may not write for example shapes.size().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM