这是我的文件:
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&lt;&lt;(unsigned int)&#39;的未定义引用 (它的时间要长得多,但我认为所有这些都与运算符的重载有关&lt;&lt;)
我做错了什么?
完整错误:
/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
答案 0 :(得分:0)
使用g++
编译代码。如果您使用的是gcc
,请在最后添加-lsdtc++
。
g++ student.cpp
或
gcc student.cpp -lstdc++