Thread: vectors and C++11

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    vectors and C++11

    What the heck I thought I could use the new for statement like this. In my book it says it works but it fails for me. What am I missing?

    Code:
    #include <iostream>
    #include <vector>
    #include <stdexcept>
    #include <iomanip>
    
    using namespace std;
    
    void inputVector(const vector<int> &array)
    {
        for( int &item : items) //ERROR undeclared "items"
            cin>>item;
        
    }
    void outputVector(vector<int> &array)
    {
        for(int item : items)  //ERROR undeclared "items"
            cout<< item <<" ";
        cout << endl;
    
    }
    int main(int argc, const char * argv[])
    {
    
        vector<int> integers1(4);
        vector<int> integer2(5);
        
        cout<<"Enter some numbers"<<integers1.size() <<endl;
        
        inputVector(integers1);
        inputVector(integer2);
        
        outputVector(integers1);
        outputVector(integer2);
        
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Isn't the error message clear enough? There is no variable named "items" (there is only one named "array"). You have also another error in your code, regarding vectors' const-qualifiers.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    maybe it is because your vector is called array and not items?

    and how do you plan to modify const vector?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Ok i moved the const to the output, I knew better then that.

    To be honest this code is completely honest this code is pretty much plagiarized. I like to take the code from my textbook and play with it. I get a better understanding of what its really doing. Thats why I am confused. I know I didn't declare items anywhere but my book didn't either. I'll go back and stare at it some more.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Works... Not sure why the textbook said otherwise. Thanks for the help!!!

    Code:
     #include <iostream>
    #include <vector>
    #include <stdexcept>
    #include <iomanip>
    
    using namespace std;
    
    void inputVector(vector<int> &array)
    {
        for( int &item : array)
            cin>>item;
        
    }
    void outputVector(const vector<int> &array)
    {
        for(int item : array)
            cout<< item <<" ";
        cout << endl;
    
    }
    int main(int argc, const char * argv[])
    {
    
        vector<int> integers1(4);
        vector<int> integer2(5);
        
        cout<<"Enter some numbers "<<integers1.size() <<endl;
        
        inputVector(integers1);
        inputVector(integer2);
        
        outputVector(integers1);
        outputVector(integer2);
        
        
        return 0;
    }

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    A non-vector comment.
    Couldn't the message for getting the user to input be something like this:
    Code:
    cout << "Enter " << integers1.size() << " numbers" << endl;
    Note also that you could use '\n' instead of std::endl. (std::endl vs “\n”)
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors in vectors - pointers and iterators
    By fisherking in forum C++ Programming
    Replies: 8
    Last Post: 07-27-2010, 09:34 AM
  2. About vectors.
    By KgNe in forum C++ Programming
    Replies: 23
    Last Post: 09-03-2008, 10:33 PM
  3. Vectors
    By Drake in forum Game Programming
    Replies: 8
    Last Post: 01-29-2006, 03:40 PM
  4. how to use vectors in VC++6.0??
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-13-2004, 08:08 AM
  5. Vectors
    By Korn1699 in forum C++ Programming
    Replies: 5
    Last Post: 02-27-2004, 03:29 AM