c++ - 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 行:
错误:语句无法解析重载函数的地址
解决方案
对于您的代码,我遇到了问题,<
而不是<<
我假设的主要问题:
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
更复杂的方法,更方便的结果,是自己做相应的重载。
推荐阅读
- json - Mongo更新时报错:无法使用部分遍历元素
- opencsv - CsvToBean 将字符串值视为 null
- sql-server - 在预期条件的上下文中指定的非布尔类型的表达式,靠近“@where”
- javascript - 为什么 element.style 在 JS 中总是返回空?
- oracle-sqldeveloper - 可视化日期中的sql开发人员问题
- python - 使用 django 中的正则表达式按指定字段获取排序的查询集
- python - 解析具有不同日期格式的数据集
- netlogo - 有没有办法在 netlogo 中创建枚举?
- reactjs - 在 SSR 期间对 react-loadable 做出反应,而不是渲染异步页面
- c# - 在 Windows 应用商店中对我的 appx 包进行审核失败