Vector problem push_back not working.

Discussion in 'C++' started by JoeC, Apr 18, 2007.

  1. JoeC

    JoeC Guest

    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?
     
    JoeC, Apr 18, 2007
    #1
    1. Advertising

  2. JoeC wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 18, 2007
    #2
    1. Advertising

  3. JoeC wrote:
    > 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.
     
    Jacek Dziedzic, Apr 18, 2007
    #3
  4. JoeC

    JoeC Guest

    On Apr 18, 11:04 am, Jacek Dziedzic
    <> wrote:
    > JoeC wrote:
    > > 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.


    Thanks, I found a better way to do what I want.
     
    JoeC, Apr 18, 2007
    #4
  5. JoeC

    Default User Guest

    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
     
    Default User, Apr 18, 2007
    #5
  6. JoeC

    anon Guest

    Default User wrote:
    > 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.
    >
    >


    He did :p
    Except for his "comments" at the end :)
     
    anon, Apr 19, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ganesh Gella
    Replies:
    5
    Views:
    1,224
    John Harrison
    Jul 14, 2003
  2. Hitesh Bhatiya
    Replies:
    4
    Views:
    4,547
    John Harrison
    Aug 13, 2003
  3. Avi Bercovich
    Replies:
    5
    Views:
    23,672
    Evan Carew
    Jan 14, 2004
  4. Al
    Replies:
    3
    Views:
    1,592
    Jim Langston
    Dec 26, 2005
  5. Replies:
    8
    Views:
    1,960
    Csaba
    Feb 18, 2006
Loading...

Share This Page