Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

c++ - Macro for push_back giving problems

I am starting to code in c++.
I was learning macros when this happenned:

#include <bits/stdc++.h>

using namespace std;

#define PB push_back

int main() {
  vector<int> hello;
  hello.PB(3);
  hello.PB(2);
  cout << hello < "
";
}

My compiler shows, pointing to line 3:

Error: statement cannot resolve address of overloaded function


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For your code I get problems with the < instead of << and what I assume the main problem:

error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::vector<int>’)
   cout << hello << "
";

It is telling you that there is no known way to output a whole vector to cout.

The simple way to fix that is

cout << hello[0] << " " << hello[1] << "
";

This gets you an output of

3 2

The more complex way, with more convenient result, is to do the overloading yourself accordingly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...