-1
class dataReader{
private:
    ifstream gfxDataFile;
    int numVertices;
    vector<*vertexData> vertices;
public:
    dataReader();
    dataReader(string file);
    ~dataReader();
    string getLine();
    int numberOfVertices();

};

the line with the vector gives me the error

vertexData: Illegal use of this type as an expression, any help guys?

Heres the definition of vertexData

class vertexData{
private:
    float x;
    float y;
    float z;
public:
    vertexData();
    vertexData(float gx, float gy, float gz);
    ~vertexData();
    float getX();
    float getY();
    float getZ();
};
user1066113
  • 759
  • 2
  • 10
  • 17

2 Answers2

2

*vertexData should be vertexData*

Putting the * on the left means, broadly, 'try to dereference the following expression' - and of course what follows is not a valid expression (though even if it were you'd have other problems trying to use an expression inside a template argument list...). When declaring pointer types the * goes on the right of the type name.

James
  • 22,556
  • 11
  • 75
  • 125
2

Write * after the type:

vector<*vertexData> vertices;  //wrong syntax
vector<vertexData*> vertices;  //correct syntax

As a sidenote, I don't think you need a vector of pointers.

Why don't you use this:

vector<vertexData> vertices; 
Nawaz
  • 327,095
  • 105
  • 629
  • 812
  • To be honest I'm not sure. In the constructor of dataReader I will create about 20k of these objects and add them to the vector. Why would I want to use one over the other? – user1066113 Feb 26 '12 at 13:24
  • @user1066113: In case of pointer, the burden of deallocating the memory is on you, while in the latter case, you don't have to do anything with regard to memory management which is a safe-side. The bottomline is : **use `new` as little as possible**. See this topic : [**why should `new` be used as little as possible?**](http://stackoverflow.com/questions/6500313/in-c-why-should-new-be-used-as-little-as-possible) – Nawaz Feb 26 '12 at 13:33
  • @user1066113: I agree with Nawaz, it's good C++ design practice to abstract away direct memory access. – Morten Jensen Sep 27 '12 at 09:36