Vector of Structures

This is a discussion on Vector of Structures within the C++ Programming forums, part of the General Programming Boards category; Before main i create a structure: Code: struct numberinfo { int number; int AoN; }; Then i create my vector ...

  1. #1
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195

    Vector of Structures

    Before main i create a structure:

    Code:
    struct numberinfo
          {
                 int number;
                 int AoN;
          };
    Then i create my vector of type structure:
    Code:
    vector <numberinfo> Numinfo;
    And when i do things like this:
    Numinfo.AoN.push_back(amount);

    It says:
    52 C:\CODE\CS192.cpp 'class std::vector<numberinfo, std::allocator<numberinfo> >' has no member named 'number'

    i have no idea why it is doing this can some one help

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You push back elements onto the vector through push_back, so something like:

    Code:
    numberinfo test;
    Numinfo.push_back(test);
    
    // Accesss first element
    Numinfo[0].number = 100;
    
    // Bounds checking direct access (slower perhaps)
    Numinfo.at(0).number = 100;
    
    // Or, safer perhaps is to use iterators
    for(vector<numberinfo>::iterator iter = Numinfo.begin(); iter != Numinfo.end(); ++iter)
    {
      (*iter).number = 50;
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    Thanks alot man that really helped

  4. #4
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    Those operations seem to be illegal becouse when i run my program with calls like that is crashes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

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