gpt4 book ai didi

c++ - 重载运算符 << undefined reference

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:32 26 4
gpt4 key购买 nike

这是我的h文件:

class Student
{
public:
Student(std::string _name, size_t _age)
:m_name(_name), m_age(_age){}

friend std::ostream& operator<<(std::ostream& _os, const Student& _student);

private:
std::string m_name;
size_t m_age;
};

std::ostream& operator<<(std::ostream& _os, const Student& _student)
{
_os <<"Student " << _student.m_name << ", age: " << _student.m_age << std::endl;
return _os;
}

这是cpp:

#include <iostream>
#include "student.h"



int main()
{
Student s1("Anna", 13);
std::cout << s1;

return 0;
}

我遇到链接错误:未定义对 `std::ostream::operator<<(unsigned int)' 的引用(它要长得多,但我认为这一切都与运算符<<的重载有关)

我做错了什么?

完整错误:

/tmp/ccv0Mh7n.o: In function `operator<<(std::ostream&, Student const&)':
oplefshift.cpp:(.text+0x1a): 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*)'
oplefshift.cpp:(.text+0x27): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
oplefshift.cpp:(.text+0x38): 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*)'
oplefshift.cpp:(.text+0x45): undefined reference to `std::ostream::operator<<(unsigned int)'
oplefshift.cpp:(.text+0x50): 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> >&)'
oplefshift.cpp:(.text+0x56): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/ccv0Mh7n.o: In function `main':
oplefshift.cpp:(.text+0x8c): undefined reference to `std::allocator<char>::allocator()'
oplefshift.cpp:(.text+0xa4): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
oplefshift.cpp:(.text+0xc8): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
oplefshift.cpp:(.text+0xd7): undefined reference to `std::allocator<char>::~allocator()'
oplefshift.cpp:(.text+0xe6): undefined reference to `std::cout'
oplefshift.cpp:(.text+0xf6): 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> >&)'
oplefshift.cpp:(.text+0xfc): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
oplefshift.cpp:(.text+0x131): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
oplefshift.cpp:(.text+0x157): undefined reference to `std::allocator<char>::~allocator()'
/tmp/ccv0Mh7n.o: In function `__static_initialization_and_destruction_0(int, int)':
oplefshift.cpp:(.text+0x1b2): undefined reference to `std::ios_base::Init::Init()'
oplefshift.cpp:(.text+0x1c7): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccv0Mh7n.o: In function `Student::Student(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int)':
oplefshift.cpp:(.text._ZN7StudentC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEj[_ZN7StudentC5ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEj]+0x11): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/ccv0Mh7n.o: In function `Student::~Student()':
oplefshift.cpp:(.text._ZN7StudentD2Ev[_ZN7StudentD5Ev]+0xe): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/tmp/ccv0Mh7n.o:(.eh_frame+0x93): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status

最佳答案

使用g++ 编译您的代码。如果您使用的是 gcc,请在末尾添加 -lsdtc++

g++ 学生.cpp

gcc 学生.cpp -lstdc++

关于c++ - 重载运算符 << undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49705871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com