Vector problem.

This is a discussion on Vector problem. within the C++ Programming forums, part of the General Programming Boards category; The other errors are actually from similar code; warnings are from comparing unsigned int to int. The function I am ...

  1. #1
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320

    Vector problem.

    The other errors are actually from similar code; warnings are from comparing unsigned int to int. The function I am writing is to open a file at filename. This file might look something like this:

    0 0 0 2 3 4 -1
    0 0 3 4 5 -1
    0 0 6 -1

    I am just having issues pushing the int I read in into the vector. I am unsure why it is not working.

    Code:
    vector<vector<int> > Modifier_Levels;   //from the class' header
    
    void Item_Gen::Load_Modifier_Tables(string filename)
    {
        int input;
        ifstream in;
        in.open(filename.c_str());
        Modifier_Levels.push_back(vector<int>());     //add an empty row
    
        while(!in.eof())
        {
            in>>input;
            if(input != -1)Modifier_Levels.push_back(input);            //add a value to the current modifier level
            else if(!in.eof())Modifier_Levels.push_back(vector<int>());   //if there is still another level to read in add a line for it.
        }
    }
    Code:
    C:\Users\David\Desktop\Server code A\Glory\Item_Generator.cpp||In member function 'void Item_Gen::Load_Modifier_Tables(std::string)':|
    C:\Users\David\Desktop\Server code A\Glory\Item_Generator.cpp|62|error: no matching function for call to 'std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::push_back(int&)'|
    c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_vector.h|733|note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >]|
    C:\Users\David\Desktop\Server code A\Glory\Item_Generator.cpp|63|error: no matching function for call to 'std::vector<int, std::allocator<int> >::push_back(std::vector<int, std::allocator<int> >)'|
    c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_vector.h|733|note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = int, _Alloc = std::allocator<int>]|
    ||=== Build finished: 4 errors, 2 warnings ===|

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    You're using a vector of vectors.

    If you want to add a vector that's

    Modifier_Levels.push_back( <an empty vector> );

    If you want to add input to a specific vector you have to access the specific vector in Modifier_Levels first.

    Modifier_Levels[i].push_back(input);

    where i is the vector. Now if you're going to be accessing the same vector a lot, consider using iterators to access vector objects instead of indexes.

  3. #3
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    I thought a general push_back(int) pushes an int to the deepest vector in the system?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    Well, no. It works exactly like a two-dimensional, randomly accessible data structure.

  5. #5
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Alright then. Seems to have made it compile gotta go throw data at it and see if it behaves. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with std::vector
    By ldb88 in forum C++ Programming
    Replies: 2
    Last Post: 02-08-2009, 01:18 AM
  2. Vector problem.
    By And3rs in forum C Programming
    Replies: 20
    Last Post: 10-14-2008, 06:46 PM
  3. Vector Problem
    By Morgul in forum Game Programming
    Replies: 16
    Last Post: 02-25-2006, 03:27 PM
  4. vector problem
    By loopshot in forum Game Programming
    Replies: 11
    Last Post: 11-19-2005, 07:20 PM
  5. STL vector <T> problem
    By correlcj in forum C++ Programming
    Replies: 11
    Last Post: 11-06-2002, 07:18 PM

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