Mete
Mete

Reputation: 313

Trying to create a vector of objects

I'm trying to create a vector of objects, i don't know what is going wrong.

here the code

class nave {
public:
    void sx(int i); int x();
    void sy(int i); int y();
};
vector<nave> naves();
naves.push_back(nave);
cout << naves.size();

Upvotes: 0

Views: 916

Answers (3)

Branko Dimitrijevic
Branko Dimitrijevic

Reputation: 52157

Do this:

vector<nave> naves;
naves.push_back(nave());
  • The old line: vector<nave> naves(); was interpreted as a function declaration.
  • The old line: naves.push_back(nave); did not actually instantiate nave.

Upvotes: 2

MPelletier
MPelletier

Reputation: 16709

A vector is just like any other class. Declare it thus:

vector<nave> naves;

Upvotes: 4

Mahesh
Mahesh

Reputation: 34665

Change -

vector<nave> naves(); // naves() is a function declaration whose return type
                      // is vector<nave>

to

vector<nave> naves;

Upvotes: 4

Related Questions