Weird STL vector push_back problrms

This is a discussion on Weird STL vector push_back problrms within the C++ Programming forums, part of the General Programming Boards category; Hey everybody, I have a situation where I have class, one of the elements of the class is a boolean. ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    34

    Weird STL vector push_back problrms

    Hey everybody,
    I have a situation where I have class, one of the elements of the class is a boolean. I also have a vector of that class type. Here is what i do:

    vector<myclass> myv;

    myclass m1 (initialize the class with the variable set to true)
    myclass m2 (initialize the class)

    myv.push_back(m1);
    cout << myv[0] << endl;

    this prints correctly, and the boolean within a class is set to true.

    Now, I add another element:

    myv.push_back(m2);
    cout << myv[0];

    Now the printed value shows that the variable is false. Any ideas why this could be? Thank you all.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Do you have a proper copy constructor and copy assignment operator for myclass? Is that code in a function and you are passing the vector by value instead of reference?

    You should be able to make a small project with just the class and a simple main() with that code. Does the problem still happen in such a small project? If not, then the problem is likely somewhere else in your code. If it does happen, then you can post that small bit of code for us to see.

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    I'm afraid you're gonna have to supply a bit more detail than that. Can you reproduce the problem in a small compileable example?
    Last edited by ChaosEngine; 06-28-2007 at 04:15 PM. Reason: :( beaten again
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    34

    Talking

    Thank you guys. My problem was the copy constructor. I was copying everything except the value of that one variable. So when vector moves things around internally this variable does not get copied properly..I knew that, and was overlooking it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 04:52 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21