error LNK2001: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > 的解决办法
2015-04-03 18:22
585 查看
#include <iostream> #include <math.h> using namespace std; template<typename T> class P { public: P(int n=0){num=n;} P(const P<T>& copy){num=copy.num;} friend ostream&operator<<(ostream& out,P<T> & obj); private: int num; }; template<typename T> ostream&operator<< (ostream& out,P<T> & obj) { out<<obj.num; return out; } int main(void) { P<int> t(2); cout<<t; return 0; }
上述程序中定义了一个模板类,但出现了错误,如下:
1>------ 已启动生成: 项目: class, 配置: Debug Win32 ------
1>正在链接...
1>main6.obj : error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class P<int> &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$P@H@@@Z),该符号在函数
_main 中被引用
1>F:\c++\c++ problem collections\class\class\Debug\class.exe : fatal error LNK1120: 1 个无法解析的外部命令
1>生成日志保存在“file://f:\c++\c++ problem collections\class\class\class\Debug\BuildLog.htm”
1>class - 2 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
我觉得这个错误主要是重载std的<<运算符造成。解决办法有两个:
一个是直接在类模版中定义这个友元函数。如下:
#include <iostream> #include <math.h> using namespace std; template<typename T> class P { public: P(int n=0){num=n;} P(const P<T>& copy){num=copy.num;} friend ostream&operator<<(ostream& out,P<T> & obj) { out<<obj.num; return out; } private: int num; }; int main(void) { P<int> t(2); cout<<t; return 0; }另一个是:
<pre name="code" class="cpp">#include <iostream> #include <math.h> using namespace std; template<typename T> class P { public: P(int n=0){num=n;} P(const P<T>& copy){num=copy.num;} friend ostream&operator<< <>(ostream& out,P<T> & obj);//friend ostream&operator<< <T>(ostream& out,P<T> & obj); private: int num; }; template<typename T> ostream&operator<< (ostream& out,P<T> & obj) { out<<obj.num; return out; } int main(void) { P<int> t(2); cout<<t; return 0; }
在声明中要多加一个<T>或者<>,在类外定义时不用加,加上就错了。
以上就提供了两种标准的解决办法。
相关文章推荐
- 使用log4cplus时遇到的链接错误:无法解析的外部符号 "public: static class log4cplus::Logger __cdecl log4cplus::Logger::getInstance(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,
- error LNK2019: 无法解析的外部符号 "class std::vector<class std::basic_string<char,struct std::char_traits<cha
- "std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::str() const"问题的解决办法
- error LNK2001: 无法解析的外部符号 "__declspec(dllimport) class std::basic_ostream
- error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: struct std::_Wrap_alloc<class std::allocator
- error LNK2005: "public: void __thiscall std::basic_ofstream<char,struct std::char_traits<char>
- error LNK2001: 无法解析的外部符号 "public: static class std::vector<class......
- error LNK2005: "public: void __thiscall std::basic_ofstream<char,struct std::char_traits<char> >::`v
- bstr error LNK2019: 无法解析的外部符号 "char * __stdcall _com_util::ConvertBSTRToString(wchar_t *)" 的解决办法
- QT中添加Q_OBJECT时编译提示obj : error LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject const错误解决方法
- 无法解析的外部符号 "wchar_t * __stdcall _com_util::ConvertStringToBSTR(char const *)"的解决办法
- error LNK2019: 无法解析的外部符号 "public: class boost::asio::basic_stream_socket
- 无法解析的外部符号 "public: virtual struct CRuntimeClass
- 错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall chain<int>::~chain<int>(void)" (??1?$chain@H@@QAE@
- vs2010+qt4编译出现error LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject等错误
- error LNK2019: 无法解析的外部符号 __imp__UuidToStringW@8,该符号在函数 "private: class ATL::CStringT<wchar_t,class S
- error LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject const * __thiscall Widget::metaObject
- 无法解析的外部符号 "wchar_t * __stdcall _com_util::ConvertStringToBSTR(char const *)"的解决办法
- error LNK2001: 无法解析的外部符号 "public: static struct QMetaObject const
- error LNK2001: 无法解析的外部符号 "class cocos2d::Vec2 const cocos2d::CCPointZero" (?CCPointZero@cocos2d@@3VV