Search code examples
c++vectorno-match

"No match for operator>>" but I don't understand why. Could you explain me, please?


I have got the following code:

#include <iostream>
#include <vector>

using namespace std;

vector<int> Reversed(const vector<int>& v){
    
    //vector<int> v;  //[Error] declaration of 'std::vector<int> v' shadows a parameter
    int z; vector<int> copy; vector<int> working;
    working = v;
    z = working.size();
    int i;
    for (i = 0; i<=z; i++){
        working[i] = working[z];
        copy.push_back(working[i]);
    }
    return copy;
}

int main() {
    
    vector<int> v;
    cin >> v;  /*[Error] no match for 'operator>>' (operand types are 'std::istream' 
                {aka 'std::basic_istream<char>'} and 'std::vector<int>')*/
    cout << Reversed(v);
    return 0;
}

Please, explain to me why I am getting this error on line 18:

no match for operator >>`

P.s.: const & i is a prerequisite task, I cannot change it. I just need an upside-down copy of this vector.


Solution

  • It looks like you are asking the user to enter a list of numbers. std::cin (or just cin, since you've got use namespace std;) doesn't know how to receive a list of integers and turn that into a vector<int> for you.

    If you want to receive a vector from user input, I would suggest you ask the user for a list of numbers, something like:

    // replace VECTOR_LENGTH
    for (int i = 0; i < VECTOR_LENGTH; i++) {
      int num;
      cin >> num;
      v.push_back(num);
    }