【发布时间】:2022-12-14 07:30:54
【问题描述】:
我正在尝试运行以下代码:
#include <iostream>
#include <array>
struct newperson {
std::array<char, 20> name{};
int age;
};
int main() {
newperson nicolas = {
"Nicolas",
21
};
newperson martin = {
"Martin",
45
};
std::cout << nicolas.age << std::endl;
std::cout << martin.name << std::endl;
return 0;
}
,这是一个结构示例
我收到以下错误:
bast.cpp: In function 'int main()':
bast.cpp:21:19: error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'std::array<char, 20>')
21 | std::cout << martin.name << std::endl;
| ~~~~~~~~~ ^~ ~~~~~~~~~~~
| | |
| | std::array<char, 20>
| std::ostream {aka std::basic_ostream<char>}
C:/msys64/mingw64/include/c++/12.2.0/ostream:754:5: error: no type named 'type' in 'struct std::enable_if<false, void>'
是否需要一些不同的语法?毕竟,我确实采用了一个现有示例并将 C 样式数组更改为标准数组。
如果我注释掉 Martin 行,我会得到以下信息:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\msys64\tmp\ccClI6JG.o:bast.cpp:(.text+0x42): undefined reference to `std::ostream::operator<<(int)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\msys64\tmp\ccClI6JG.o:bast.cpp:(.text+0x54): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\msys64\tmp\ccClI6JG.o:bast.cpp:(.text+0x76): undefined reference to `std::ios_base::Init::~Init()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\msys64\tmp\ccClI6JG.o:bast.cpp:(.text+0xa9): undefined reference to `std::ios_base::Init::Init()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\msys64\tmp\ccClI6JG.o:bast.cpp:(.rdata$.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_[.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_]+0x0): 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> >&)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\msys64\tmp\ccClI6JG.o:bast.cpp:(.rdata$.refptr._ZSt4cout[.refptr._ZSt4cout]+0x0): undefined reference to `std::cout'
collect2.exe: error: ld returned 1 exit status
不过,这似乎是 mingw/msys2 的问题,因为它适用于 Godbolt 和 tio.run
【问题讨论】:
-
这不是mingw的问题。
std::array<char, 20>没有<<。你为什么不使用std::string? -
std::array<T>不是T[]的别名,它更像是一个安全(并且基本上同样有效)的包装器。如果你想得到指针,.data()就可以了。 -
顺便说一句,你的标题说你正在尝试打印数组的一个元素,那会起作用
-
@463035818_is_not_a_number 你是在谈论第一块错误消息吗?这可能与 mingw 无关,但肯定有问题:在尝试编译 this[(https://pst.moe/paste/lpsnaq) I get [this 时。如果我使用 g++ 而不是 gcc,我会得到
g++: fatal error: cannot execute 'cc1plus': spawn: No such file or directory compilation terminated.有趣的是,这仅在我安装 mingw-w64-x86_64-gcc 之后才开始。当我使用 msys2 的 gcc 时,它运行良好。 -
不,我只是指
std::cout << martin.name << std::endl;,这是一个错误,因为std::array没有<<。另一方面,std::cout << martin.name[0] << std::endl;可以打印数组的一个元素。我承认,我什至没有阅读您发布的错误消息
标签: c++ gcc g++ mingw-w64 msys2