c++ - 如何为向量重载 << 运算符在 C++ 中
问题描述
我为 C++ 中的运算符重载做了以下最小示例。我想打印如下所示的矢量。
#include <iostream>
#include <vector>
using namespace std;
ostream& operator<< (ostream& out, const vector<int>& v) {
for(size_t i = 0; i < v.size(); i++) {
out << v[i] << " ";
}
return out;
}
int main(void)
{
vector<int> i = {1, 2};
cout << i << endl;
}
但是,如果我使用 gcc 将其编译为gcc test.cpp
使用以下版本。
$ gcc --version
gcc (Arch Linux 9.3.0-1) 9.3.0
我到处都收到错误消息,似乎减少到以下内容。
undefined reference to `std::ostream::operator<<(int)'
但为了完整起见,这里是完整的错误消息。
/usr/bin/ld: /tmp/cc8gPHc3.o: in function `operator<<(std::ostream&, std::vector<int, std::allocator<int> > const&)':
test.cpp:(.text+0x4e): undefined reference to `std::ostream::operator<<(int)'
/usr/bin/ld: test.cpp:(.text+0x5d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: /tmp/cc8gPHc3.o: in function `main':
test.cpp:(.text+0xec): undefined reference to `std::cout'
/usr/bin/ld: test.cpp:(.text+0xfb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/usr/bin/ld: test.cpp:(.text+0x106): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/usr/bin/ld: /tmp/cc8gPHc3.o: in function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x195): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: test.cpp:(.text+0x1aa): undefined reference to `std::ios_base::Init::~Init()'
/usr/bin/ld: /tmp/cc8gPHc3.o: in function `std::vector<int, std::allocator<int> >::_S_check_init_len(unsigned long, std::allocator<int> const&)':
test.cpp:(.text._ZNSt6vectorIiSaIiEE17_S_check_init_lenEmRKS0_[_ZNSt6vectorIiSaIiEE17_S_check_init_lenEmRKS0_]+0x5e): undefined reference to `std::__throw_length_error(char const*)'
/usr/bin/ld: /tmp/cc8gPHc3.o: in function `__gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long)':
test.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim[_ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim]+0x1c): undefined reference to `operator delete(void*)'
/usr/bin/ld: /tmp/cc8gPHc3.o: in function `__gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*)':
test.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv]+0x2c): undefined reference to `std::__throw_bad_alloc()'
/usr/bin/ld: test.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv]+0x3c): undefined reference to `operator new(unsigned long)'
/usr/bin/ld: /tmp/cc8gPHc3.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
解决方案
使用g++
而不是gcc
解决问题。
推荐阅读
- .net - .NET 框架 4.7.2。未安装(并且没有错误消息)
- javascript - JSON.stringify() 的时间复杂度是多少?
- java - 有没有办法在 Java 中推断泛型类型?
- asp.net - IBM Softlayer (Cloud Load Balancer + SSL OffLoad) ASP.NET too many redirects 错误
- python - python - ctypes opengl函数加载错误(OSError:异常:访问冲突写入0x0000000000000000)
- apache-spark-sql - Spark sql 子查询
- ansible - 将文件夹的全部内容(包括子文件夹)复制到目标
- c++ - C++ MEX 中具有特征矩阵运算的 OpenMP
- javascript - 在 React 中实现分页,每页显示来自 JSON 文件的 15 个项目
- python - 无法使用 errbot 命令创建聊天室,导致 not_allowed_token_type 错误