本文发表在 rolia.net 枫下论坛I write a small code for try template, the coding is :
head file:
template<class T>
class max_get
{
T fisrt;
T second;
public:
max_get();
max_get(T value1, T value2);
virtual ~max_get();
T maxvalue();
};
.cpp file:
template<class T>
max_get<T>::max_get()
{
}
template<class T>
max_get<T>::max_get(T value1, T value2)
{
first=value1;
second=value2;
}
template<class T>
max_get<T>::~max_get()
{
}
template<class T>
T max_get<T>::maxvalue()
{
if (first>second)
return first;
else
return second;
}
main file:
int main(int argc, char* argv[])
{
max_get<int> intmax(10,15);
if (intmax.maxvalue()==10)
cout<<"max=10\n"<<endl;
else
cout<<"max=15\n"<<endl;
return 0;
}
the above code has passed the compiling in VC++ studio. but when build .exe file, there are the following problem happen:
template_max.cpp
max_get.cpp
Generating Code...
Linking...
template_max.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall max_get<int>::~max_get<int>(void)" (??1?$max_get@H@@UAE@XZ)
template_max.obj : error LNK2001: unresolved external symbol "public: int __thiscall max_get<int>::maxvalue(void)" (?maxvalue@?$max_get@H@@QAEHXZ)
template_max.obj : error LNK2001: unresolved external symbol "public: __thiscall max_get<int>::max_get<int>(int,int)" (??0?$max_get@H@@QAE@HH@Z)
Debug/template_max.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.
template_max.exe - 4 error(s), 0 warning(s)
pls tell me how to do it, thanks. SOS更多精彩文章及讨论,请光临枫下论坛 rolia.net
head file:
template<class T>
class max_get
{
T fisrt;
T second;
public:
max_get();
max_get(T value1, T value2);
virtual ~max_get();
T maxvalue();
};
.cpp file:
template<class T>
max_get<T>::max_get()
{
}
template<class T>
max_get<T>::max_get(T value1, T value2)
{
first=value1;
second=value2;
}
template<class T>
max_get<T>::~max_get()
{
}
template<class T>
T max_get<T>::maxvalue()
{
if (first>second)
return first;
else
return second;
}
main file:
int main(int argc, char* argv[])
{
max_get<int> intmax(10,15);
if (intmax.maxvalue()==10)
cout<<"max=10\n"<<endl;
else
cout<<"max=15\n"<<endl;
return 0;
}
the above code has passed the compiling in VC++ studio. but when build .exe file, there are the following problem happen:
template_max.cpp
max_get.cpp
Generating Code...
Linking...
template_max.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall max_get<int>::~max_get<int>(void)" (??1?$max_get@H@@UAE@XZ)
template_max.obj : error LNK2001: unresolved external symbol "public: int __thiscall max_get<int>::maxvalue(void)" (?maxvalue@?$max_get@H@@QAEHXZ)
template_max.obj : error LNK2001: unresolved external symbol "public: __thiscall max_get<int>::max_get<int>(int,int)" (??0?$max_get@H@@QAE@HH@Z)
Debug/template_max.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.
template_max.exe - 4 error(s), 0 warning(s)
pls tell me how to do it, thanks. SOS更多精彩文章及讨论,请光临枫下论坛 rolia.net