-1

I want to pass to a function a std::vector but i wont want to copy the whole vector. I just want to pass the adress to the vector. So i try to use a pointer and i find out that i cant use the brackets to acces which element from the vector i want. How do i do that? This come gives me the error " cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'"

#include <iostream>
#include <vector>

using namespace std;

void f(vector<int>* x);

int main()
{
    vector<int>a;
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);
    a.push_back(4);
    a.push_back(5);
    f(&a);
    return 0;
}

void f(vector<int>* x)
{
    int i;
    for(i=0;i<x->size();i++)
        cout<<x[i];
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 6
    *I want to pass to a function a std::vector but i wont want to copy the whole vector* then you want to use a reference, not a pointer. It makes this problem go away. – NathanOliver Sep 28 '17 at 17:36
  • 2
    You can use `x->at(i)`. But using a reference is better, as @NathanOliver says. – Barmar Sep 28 '17 at 17:38
  • 1
    Or "cout<< (*x)[i]" – Larry Sep 28 '17 at 17:39
  • x is a pointer to a `vector`. Therefore, `x[i]` gives you a `vector`, and not just a plain `int`. You can't use a pointer to access something in exactly the same way as if it were the object itself. That's what a pointer means, after all. It's not the same object. It's a pointer to the object. – Sam Varshavchik Sep 28 '17 at 17:39
  • I get a completely different error. – Kerrek SB Sep 28 '17 at 17:40
  • Pass it as const reference, as in the code. Passing by reference is the same as using a pointer, except language syntax prevents from passing a null pointer to the function. void f(const vector& x); int main() { vectora; a.push_back(1); a.push_back(2); a.push_back(3); a.push_back(4); a.push_back(5); f(a); return 0; } void f(const vector& x) { int i; for (i = 0; i< x.size(); i++) cout << x[i] << '\n'; } – Uri Raz Sep 28 '17 at 17:42
  • See my answer here: https://stackoverflow.com/questions/7677007/passing-vector-to-function-c/7677043#7677043 – Chad Sep 28 '17 at 17:55

2 Answers2

3

Change this:

cout << x[i];

to this:

cout << (*x)[i];

or even better this:

cout << x->at(i);

since x is a pointer to the vector.

But didn't you know already? I mean you used x->size(), and not x.size()...

However, why are you passing a pointer to the function? A reference would be fine, and would allow the body of your function as it stands in your question to work fine. And since you are just printing, a const reference, would be nice, like this:

void f(const vector<int>& x)
{
   // same body as in your question
}

Unrelated to your problem, but your compiler should generate a warning for your for loop, of this kind:

warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type {aka long unsigned int}' [-Wsign-compare]
     for(i=0;i<x->size();i++)
             ~^~~~~~~~~~

Fix it by declaring i as an unsigned int, or size_t.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    `at` and `operator[]` aren't exactly equivalent. For `std::vector` the `at` method does bounds checking. The distinction is more significant for other containers such as `std::map` where `at` and `operator[]` are not always interchangeable. One is `const` and the other default constructs values when they aren't found. – François Andrieux Sep 28 '17 at 17:47
  • Indeed @FrançoisAndrieux, thanks for the comment. Do you suggest I modify somehow my answer? If yes, how? – gsamaras Sep 28 '17 at 18:12
  • 1
    `x->at(i);` is a fine suggestion for clarity. But since the first two examples are identical in behavior, using `x->operator[](i);` would be more consistent with the other examples. Now, rather clarify or consistency is more important is up to you. I personally don't think any change is needed. – François Andrieux Sep 28 '17 at 18:15
0

You should pass const reference in this case, instead of raw pointer:

void f(const vector<int> &x)
{
    int i;
    for(i=0;i<x.size();i++)
        cout<<x[i];
}

and call it without taking address:

f(a);

This would also eliminate another problem in your code, you should check passed pointer to f() for nullptr before dereferencing it.

Slava
  • 43,454
  • 1
  • 47
  • 90