0

I am trying to print a vector of integers to the terminal using "cout", but I get an error message during compiling:

no match for 'operator<<' (operand types are 'std::basic_ostream' and 'std::vector') cout << "Disparity at points: " << disparityVector << endl;

The snippet of code looks like this:

vector<int> disparityVector;

for ( int i=0; i<drawPixels.size(); i++)    // Get disparity at each point of the drawn line
  disparityVector.push_back((int)disparityMapOutput.at<int16_t>(pos[i].y, pos[i].x));

cout << "Disparity at points: " << disparityVector << endl;

There is no error with assigning the values to the vector, only the "cout" part of the code is making errors

eirikaso
  • 123
  • 15

4 Answers4

5

For example, using ostream_iterator.

Sample from that page:

// ostream_iterator example
#include <iostream>     // std::cout
#include <iterator>     // std::ostream_iterator
#include <vector>       // std::vector
#include <algorithm>    // std::copy

int main () {
  std::vector<int> myvector;
  for (int i=1; i<10; ++i) myvector.push_back(i*10);

  std::ostream_iterator<int> out_it (std::cout,", ");
  std::copy ( myvector.begin(), myvector.end(), out_it );
  return 0;
}
wl2776
  • 4,099
  • 4
  • 35
  • 77
2

You'll need something like below, if you want to do it the way you've coded it.

template<class T>
inline std::ostream& operator<< (std::ostream& o, std::vector<T> const& v) {
  for (auto const& i : v)
    o << i << " ";
  return o;
}
Kaveh Vahedipour
  • 3,412
  • 1
  • 14
  • 22
2
std::ostream& operator<<(std::ostream& os, const vector<int>& v)
{
    std::ostream_iterator<int> _oit(cout, " ");
    std::copy(v.begin(), v.end(), _oit);
    return os;
}
rsy56640
  • 299
  • 1
  • 3
  • 13
0

This error means that vector<T> has no operator<< implementation. You need to iterate over your vector and print each element:

for(int i = 0; i < disparityVector.size; i++) {
    cout << disparityVector[i];
}
cout << endl;
Rafiwui
  • 544
  • 6
  • 19