c++ - Microsoft 重载 << 运算符示例会引发链接错误

标签 c++ visual-studio operator-overloading

我正在尝试编译 Microsoft 示例“Overloading the << Operator for Your Own Classes”但出现以下链接错误:

error LNK1169: one or more multiply defined symbols found

error LNK2005: "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class Date const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVDate@@@Z) already defined in Date.obj

有人知道为什么这没有编译吗?

最佳答案

如果您在头文件中定义了运算符,则必须将其声明为内联,否则它将在包含该头文件的所有翻译单元中定义。

但最好将它移到一个实现文件中,除非您有充分的理由将它放在标题中。

关于c++ - Microsoft 重载 << 运算符示例会引发链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9053514/

相关文章:

C++赋值运算符重载

c++ - 如何使用 Windows 服务启动程序?

visual-studio - 从 Visual Studio Express 发布

C++ 重载 : operator+ for adding Matrices by element

c++ - 程序输出的随机数序列与 Linux 上的实际 rand() 输出不匹配(在 Windows 上工作正常)

c++ - 是否为派生类生成移动构造函数/赋值运算符

c++ - OpenGL:使用着色器通过使用预先计算的颜色图来创建顶点照明?

c# - Visual Studio 2010 调试器中的上下文变量

C++ 如何实现:myInstance << "abc"<< someInt << std::endl

operator-overloading - Smalltalk 中的运算符可以重载吗?