Thread: Class/Struct in a Vector...

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up Class/Struct in a Vector...

    I'm trying to put a struct or a class into a vector... it seems to work alright but I would like to know how to use functions/variables from the class.

    Code:
    struct test
    {
    	int aaa;
    };
    
    int main()
    {
    	vector<test> vec;
    	test str;
    	str.aaa=12;
    	vec.push_back(str);
    	cout<<vec[vec.size()].aaa;
    }
    This attempt compiles but comes up with 6 digit numbers when I expect it to be 12. So how do you put a class into a vector and retrieve the information?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    The problem is that vec[vec.size()] accesses an element that does not exist (it is one past the last element in the array).

    What you really want to write is:
    Code:
    cout << vec[0].aaa;
    Or if you want the most recently pushed element:
    Code:
    cout << vec.back().aaa;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Code:
        cout<<vec.size() << endl;
        cout<<vec[vec.size()-1].aaa << endl;
    Just like arrays, subscripts start at 0.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Ah! Thankyou both alot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM