jeudi 2 janvier 2020

Cout does not work - (error: no match for ‘operator<<�’)

I am trying to learn c++. I have understood few things.

I was trying to implement program using vector in c++ for dynamic array, while everything seems to work but one thing in particular cout statement throws some weird error .

#include <iostream> 
#include <vector> 

using namespace std; 

int main() 
{ 
    vector<int> g1; 

    for (int i = 1; i <= 5; i++) {
        g1.push_back(i); 
    }

    cout << "This works"; // this cout works

    cout << g1; // but this does not why ?

    return 0;
}


Error that I am getting after running.

main.cpp: In function ‘int main()’:
main.cpp:18:7: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::vector<int>’)
  cout << g1;
  ~~~~~^~~~~

Here is the program. I was trying to debug on hackerrank and I came across this problem. Why does not cout work for vector variable only? What am I missing?

Aucun commentaire:

Enregistrer un commentaire