<转自“软件工匠笔记”:http://linhs.blog.51cto.com/,特别表示感谢,希望更多被C++模板所折磨的人能看到这篇文章,写得很清楚透彻>
#ifndef _REC_H_
#define _REC_H_
class Rec
{
public:
Rec(
int a);
void show();
private:
int m_a;
};
#endif
#include <iostream>
using namespace std;
Rec::Rec( int a)
{
m_a = a;
}
void Rec::show(){
printf( "item=%d/n",m_a);
}
void main()
{
Rec a(10);
a.show();
}
#ifndef _RECT_H_
#define _RECT_H_
template < class T>
class RecT{
public:
RecT(T t);
void show();
private:
T m_a;
};
#endif //! #ifndef _RECT_H_
#include <iostream>
using namespace std;
template < class T>
RecT<T>::RecT(T t)
{
m_a = t;
}
template < class T>
void RecT<T>::show(){
cout<< "item="<<m_a<<endl;
}
void main()
{
RecT< int> a(10);
a.show();
}
Test.cpp
Linking...
Test.obj : error LNK2001: unresolved external symbol "public: void __thiscall RecT<int>::show(void)" (?show@?$RecT@H@@QAEXXZ)
Test.obj : error LNK2001: unresolved external symbol "public: __thiscall RecT<int>::RecT<int>(int)" (??0?$RecT@H@@QAE@H@Z)
Debug/TemplateTest.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
void main()
{
RecT< int> a(10);
a.show();
}
519



被折叠的 条评论
为什么被折叠?



