Welcome to OGeek Q&A 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...