////////////////////////template.h
#ifndef TEMPLATETEST_H
#define TEMPLATETEST_H
templateclass TemplateTest
{
public:
TemplateTest(){};
T AddNumber(T a,T b);
};
#endif // TEMPLATETEST_H
////////////////////////template.cpp
#include "templatetest.h"
template T TemplateTest::AddNumber(T a,T b)
{
return (a+b);
}
/////////////////////////////////////main.cpp
#include
#include "templatetest.h"
using namespace std;
int main()
{
TemplateTest tt;
cout<<tt.AddNumber(10,20)<<endl;
return 0;
}
存在疑问:
编译时系统报错 "undefined reference to `TemplateTest::AddNumber(int, int)' "
而将#include "templatetest.h"改为#include "template.cpp"
编译无报警,运行结果正确;
苦思不得其解,还请大神们能够指点一下
注:
Qt版本5.6.2
mingw版本4.9.2