关于模版类 写构造函数之后无法编译的问题

模版类添加构造函数编译出错  错误提示: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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值