模版类添加构造函数编译出错 错误提示:Error 1 error LNK2001: unresolved external symbol "public: __thiscall Store<int>::Store<int>(void)" (??0?$Store@H@@QAE@XZ)
Error 2 error LNK1120: 1 unresolved externals
但是不加构造函数却可以运行,
查资料才知道编译器不支持模版类定义域实现分离,网上有些把定义与实现剪到一个文件下便已通过了。但是我的剪到一个文件下依然没有便已通过,后来又有是在函数前间export,但是08以上版本编译器不支持export。我试了一下,构造函数前加export编译没有通过,删掉export之后却可以运行了,现在还不知道原因 先记录一下
模版类测试:
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<stdio.h>
#include<cstdlib>
using namespace std;
template<class T>
class Store{
private:
T element;
bool haveValue;
public:
Store(){haveValue=false;}
T &getElement()
{
if(!haveValue)
{
cout<<"there is none value"<<endl;
exit(1);
}
return element;
}
void setElement(const T &elm)
{
haveValue=true;
element=elm;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Store<int>s1;
int a ;
s1.setElement(3);
cout<<s1.getElement()<<endl;
getchar();
return 0;
}