×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

who can help me overcome this problem for c++ template. SOS

本文发表在 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
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / who can help me overcome this problem for c++ template. SOS
    本文发表在 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
    • 象是工程文件不对,把程序全部方到你的main.cpp里试试
      • would you try it?
        I used the vc++ to create a project with console application, then create a class with a template, in main function, i call them. would you check it? do you have the experience of developing the c++ program with temp
    • most c++ compilers don't support separated head and implementation of template. Always put them into same file!
    • move the definition of your template class from .cpp to .h file.