Vector Errors

This is a discussion on Vector Errors within the C++ Programming forums, part of the General Programming Boards category; It's odd....every attempt I've made at vectors with this code has worked, except this one. I'm really not seeing what's ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    220

    Vector Errors

    It's odd....every attempt I've made at vectors with this code has worked, except this one. I'm really not seeing what's wrong with it.

    [I]Dev-C++ Error Report[/I}
    D:\Dev-Cpp\vectors.cpp In function `int main(int, char**)':
    13 D:\Dev-Cpp\vectors.cpp invalid conversion from `const char*' to ` char'
    13 D:\Dev-Cpp\vectors.cpp initializing argument 1 of `void std::basic_string<_CharT, _Traits, _Alloc>:ush_back(_CharT) [with _CharT =
    ...
    16 D:\Dev-Cpp\vectors.cpp no `operator++(int)' declared for postfix `++ ', trying prefix operator instead
    16 D:\Dev-Cpp\vectors.cpp no match for 'operator++' in '++ vstr_Inventory'
    Code:
    ///**********************************************************///
    /*********************VECTOR-EXPERIMENTS***********************/
    ///**********************************************************///
    
    // Project: Vector-Experiments
    // Compiler: Mingw Port of GCC
    // IDE: Dev-C++ 4.9.9.0
    // Operating System: Win Pro CE
    // File: main.cpp
    // Description: An attempt to write an inventory script, this is just
    //  example code from the original file, but all the code is the
    //  same anyways besides the vector size.
    
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char **argv)
    {
      //Vector size is normally 10, but reduced to 3 for speed / 
      // demonstration purposes.
      vector<string> vstr_Inventory(3);
       vector<string>::iterator iter_vstr_Inventory = vstr_Inventory.begin();
       vstr_Inventory[0].push_back("Sacrament of the Dead");
       vstr_Inventory[1].push_back("Revealing Light");
       vstr_Inventory[2].push_back("Revitalizing Flask");
       for(iter_vstr_Inventory; iter_vstr_Inventory <= vstr_Inventory.end(); vstr_Inventory++)
       {
         std::cout << "The item in slot x is:" << *iter_vstr_Inventory;
       }
       system("pause");
       return 0;
     }
    It's odd...I don't see what's wrong with vstr_Inventory[0].push_back("string here.."); I tried taking out the spaces if it didn't like that(even if the errors said otherwise, seems to work sometimes), but of course to no avail. Those errors are just too cryptic for me, and I don't see how it wants a const char...isn't that what I'm giving it? o_0
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Code:
       vstr_Inventory[0].push_back("Sacrament of the Dead");
       vstr_Inventory[1].push_back("Revealing Light");
       vstr_Inventory[2].push_back("Revitalizing Flask");
    You're getting errors because you're pushing back a const char* on to a string.

    By doing vstr_Inventory[0], you're grabbing the first string variable, and not setting the actual vector class.

    Do this instead:

    Code:
       vstr_Inventory[0]="Sacrament of the Dead";
       vstr_Inventory[1]="Revealing Light";
       vstr_Inventory[2]="Revitalizing Flask";
    Also, the for loop should be:
    Code:
    iter_vstr_Inventory < vstr_Inventory.end()
    As in your code, you're reading one more inventory item than you need.
    Last edited by jverkoey; 03-23-2005 at 05:58 PM.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Describe vstr_Inventory. Is that a vector of string, or an array of vector of string?

    Kuphryn

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    Your for loop should look something like this:

    vector<string>::const_iterator iter_vstr_Inventory = vstr_Inventory.begin();
    for(; iter_vstr_Inventory != vstr_Inventory.end(); ++vstr_Inventory)

    The const_ is not necessary depending on what you will do with the string. You should always use != to compare iterators. You should generally prefer ++x to x++ if you aren't using the return value.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Quote Originally Posted by kuphryn
    Describe vstr_Inventory. Is that a vector of string, or an array of vector of string?

    Kuphryn
    It's a vector of type string.

    Thanks Dave I was thinking of using const_iterator but it seemed it didn't like vector<string>::iterator for some reason o_o
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

Popular pages Recent additions subscribe to a feed

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