首页 > 解决方案 > push_back 给出问题的宏

问题描述

我开始用 C++ 编写代码。
发生这种情况时我正在学习宏:

#include <bits/stdc++.h>

using namespace std;

#define PB push_back

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

我的编译器显示,指向第 3 行:

错误:语句无法解析重载函数的地址

标签: c++

解决方案


对于您的代码,我遇到了问题,<而不是<<我假设的主要问题:

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

它告诉您没有已知的方法可以将整个向量输出到cout.

解决这个问题的简单方法是

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

这会给你一个输出

3 2

更复杂的方法,更方便的结果,是自己做相应的重载。


推荐阅读