-1

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Ram Rai
  • 19
  • 3
  • 1
    Thankfully the standard library doesn't contain a buildin way of reading a whole vector of something. See also this question: https://stackoverflow.com/questions/26589317/how-to-read-entire-stream-into-a-stdvector – Lukas-T Oct 15 '20 at 19:41
  • 1
    Ask yourself this question. How do you think this code `cin >> v;` is going to know how big the vector is? Hopefully that makes it obvious that you cannot read a vector that way. If you need to read a vector then write a loop, and then you can decide how big to make the vector. – john Oct 15 '20 at 19:41
  • What exactly are you attempting to do? I can see multiple out of range index errors in your function... – Anonymous1847 Oct 15 '20 at 19:42
  • The error message is telling you that `vector` does not include that functionality. There are many ways to read a series of ints into a vector but that isn't one of them.Worth noting, `working[i] = working[z];` looks like it accesses the vector out of bounds. – Retired Ninja Oct 15 '20 at 19:42
  • Don't pre-declare your variables. Declare them when you initialize them. – JHBonarius Oct 15 '20 at 19:42
  • Also please use full indentation for each scope level. – Anonymous1847 Oct 15 '20 at 19:42
  • `std::reverse()` – sweenish Oct 15 '20 at 19:44
  • @churill thank you) – Ram Rai Oct 15 '20 at 19:45
  • ^ std::reverse() Yup. Plus I don't think your reverse function would work, even apart from the out of range errors. Calling push_back for each element of the input in order would just make the same vector in the same order. – Anonymous1847 Oct 15 '20 at 19:45
  • @John very thank you for your help! and thanks for all, honestly :) Now I understand what the problem is and can try to solve it! – Ram Rai Oct 15 '20 at 19:46

2 Answers2

3

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);
}
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
ajm
  • 144
  • 7
  • Very thanks! I'm new, so I didn’t think that does not take values through "cin >>" – Ram Rai Oct 16 '20 at 08:29
  • Looking at what the error message in your code was saying, it isn't terribly indicative about the error you made. It has to do with how `cin >>` and `cout <<` are implemented by overloading the shift operators, and that isn't necessarily helpful someone who's new! – ajm Oct 16 '20 at 21:47
2

You are trying to read the whole vector in one go when you do this cin >> v;. You probably want to read one element at a time, like:

#include <iostream>
#include <vector>

using namespace std;

int main(void)
{
   // read five integers from stdin
  const int n = 5;
  vector<int> v(n);
  for(int i = 0; i < n; ++i)
    cin >> v[i];
  for(int i = 0; i < n; ++i)
    cout << v[i] << "\n";
  return 0;
}

Output:

0
1
2
3
4

Or use std::vector::push_back as in @ajm answer.

gsamaras
  • 71,951
  • 46
  • 188
  • 305