Vector problem push_back not working.

J

JoeC

I am doing some routine code with dev C++ and I am getting an error
when I am trying to basic functions with a vector.

std::vector<nation>pl;

nation n1(red); <these are constructors.
nation n2(black);

int z = pl.size(); <--this works just.

pl.push_back(n1); <--get error here.

36 C:\Documents and Settings\Work\My Documents\C++\SBGIII\Main.cpp
expected constructor, destructor, or type conversion before '.' token
36 C:\Documents and Settings\Work\My Documents\C++\SBGIII\Main.cpp
expected `,' or `;' before '.' token

This does not make any sense, I use vectors all the time and why is
the compiler giving me problems?
 
V

Victor Bazarov

JoeC said:
I am doing some routine code with dev C++ and I am getting an error
when I am trying to basic functions with a vector.

std::vector<nation>pl;

A declaration of a vector at namespace scope is OK. It's default-
initialised.
nation n1(red); <these are constructors.
nation n2(black);

Two declarations (definitions) at namespace scope with respective
initialisations are OK.
int z = pl.size(); <--this works just.

It's still a declaration/definition with a copy-initialiser. OK.
pl.push_back(n1); <--get error here.

Now, this is not a declaration. It's an executable statement. It
is not OK to have an exectutable statement OUTSIDE of any function.
36 C:\Documents and Settings\Work\My Documents\C++\SBGIII\Main.cpp
expected constructor, destructor, or type conversion before '.' token
36 C:\Documents and Settings\Work\My Documents\C++\SBGIII\Main.cpp
expected `,' or `;' before '.' token

This does not make any sense, I use vectors all the time and why is
the compiler giving me problems?

Because you're outside of any function, most likely. Check your
curly braces.

V
 
J

Jacek Dziedzic

JoeC said:
I am doing some routine code with dev C++ and I am getting an error
when I am trying to basic functions with a vector.

std::vector<nation>pl;

nation n1(red); <these are constructors.
nation n2(black);

int z = pl.size(); <--this works just.

pl.push_back(n1); <--get error here.

36 C:\Documents and Settings\Work\My Documents\C++\SBGIII\Main.cpp
expected constructor, destructor, or type conversion before '.' token
36 C:\Documents and Settings\Work\My Documents\C++\SBGIII\Main.cpp
expected `,' or `;' before '.' token

This does not make any sense, I use vectors all the time and why is
the compiler giving me problems?

A missing semicolon on the previous line?
Or "p1" vs. "pl"?
Or an extra '}' on a previous line?

HTH,
- J.
 
D

Default User

JoeC wrote:

This does not make any sense, I use vectors all the time and why is
the compiler giving me problems?


Post a complete, minimal program that demonstrates the problem.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top