输出结构体中std::array成员报错及MinGW链接问题咨询
问题描述
我尝试运行以下结构体示例代码:
#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风格数组改成std::array得到的,是不是需要用不同的语法?
如果注释掉和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上能正常运行,请问该怎么解决这些问题?
解决方案
1. std::array<char, N> 输出问题的解决
C++标准库并未为std::array提供operator<<的重载,因此无法直接通过std::cout输出std::array<char, 20>对象,有两种可行的解决方式:
调用
data()获取底层指针std::array的data()成员函数会返回指向首元素的指针,由于你用字符串字面量初始化数组,内部会自动以'\0'结尾,因此可以像输出普通C字符串一样操作:std::cout << martin.name.data() << std::endl;遍历数组输出字符
如果数组内容不以'\0'结尾,可以手动遍历每个字符直到遇到终止符:for (char c : martin.name) { if (c == '\0') break; std::cout << c; } std::cout << std::endl;
2. MinGW/MSYS2 链接错误的解决
这类“undefined reference”错误本质是编译时未正确链接C++标准库,解决方法如下:
使用
g++命令编译gcc是C语言编译器,默认不会链接C++标准库libstdc++,改用g++编译即可自动处理:g++ bast.cpp -o bast.exe显式指定链接标准库
若g++仍报错,可手动添加链接选项:gcc bast.cpp -o bast.exe -lstdc++修复MinGW环境
若以上方法均无效,可能是MSYS2的MinGW组件损坏,执行以下命令更新或重装gcc:pacman -Syu mingw-w64-x86_64-gcc
内容的提问来源于stack exchange,提问作者gps
相关产品推荐
相关产品推荐

