0

Here is the error I am getting: Exercise11.cxx:29:13: error: invalid operands to binary expression ('ostream' (aka 'basic_ostream') and 'vector')

#include <iostream> 
#include <vector>
#include <iomanip>

using namespace std;
int main()
{
   cout << "Kaitlin Stevers" << endl;
   cout << "Exercise 11 - Vectors" << endl;
   cout << "November 12, 2016" <<endl;
   cout << endl;
   cout << endl;
   int size;
   cout << " How many numbers would you like the vector to hold? " << endl;
   cin >> size;
   vector<int> numbers;
   int bnumbers;

   for (int count = 0; count < size; count++)
   {
       cout << "Enter a number: " << endl;
       cin >> bnumbers;
       numbers.push_back(bnumbers); 
    }
    //display the numbers stored in order
    cout << "The numbers in order are: " << endl;
    for(int i=0; i < size; i++)
    {
        cout<<numbers[i]<< " ";
    }
    cout << endl;
   return 0;
}

The error comes up on the part of the code that says:

cout << numbers << endl;

Second question:

How do I use vent.reverse(); to reverse the vector and then display it.

Katie Stevers
  • 135
  • 1
  • 1
  • 13

4 Answers4

1

The error tells you all you need to know: operator<< is not defined between std::cout and std::vector.

The line that fails is this one...

cout << numbers << endl;

...because cout is a ostream and numbers is a vector. Here's a list of supported types that can be streamed into ostream.


Consider using a for loop to print out the contents of numbers:

cout << "The numbers in order are: " << endl;

for(const auto& x : numbers)
{ 
    cout << x << " ";
}

cout << endl;

If you do not have access to C++11 features, strongly consider researching them and updating your compiler. The code below if C++03-compliant:

cout << "The numbers in order are: " << endl;

for(std::size_t i = 0; i < numbers.size(); ++i)
{ 
    cout << numbers[i] << " ";
}

cout << endl;
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
1

You cannot call cout << numbers because there is no defined way to output a vector. If you want to print out the data in a vector you need to iterate through and print each element separately. More information is available How to print out the contents of a vector?

Community
  • 1
  • 1
1

You have to use a loop to print the vector:

for(int i=0; i < size; i++){
    cout<<numbers[i]<< " ";
}
Mirko
  • 75
  • 1
  • 9
1

Another way, using C++11 features

for (auto it = numbers.begin(); it < numbers.end(); it ++)
    cout << (*it) << "\t";
jorge saraiva
  • 235
  • 4
  • 15