C++primer的一个问题,,求教。。。。 (

happycose 2007-03-09 05:14:45
C++primer第2章2。5泛型设计的例子,不知道怎么了,调试报出
1.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall
Array<char>::Array<char>(int)" (??0?$Array@D@@QAE@H@Z),该符号在函数 _wmain
中被引用
1.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall
Array<double>::Array<double>(int)" (??0?$Array@N@@QAE@H@Z),该符号在函数
_wmain 中被引用
1.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall
Array<int>::Array<int>(int)" (??0?$Array@H@@QAE@H@Z),该符号在函数 _wmain 中
被引用
的错误。。请问是怎么回事。。。谢谢。。。。
template <class elemType>
class Array{
public:
explicit Array(int size=DefaultArraySize);
Array(elemType *Array,int Array_size);
Array(const Array &rhs);

~Array() {delete []ia;}

bool operator==(const Array&) const;
bool operator!=(const Array&)const;
Array& operator=(const Array&);
int size() const{return _size;}

elemType &operator[](int index){return ia[index];}
void sort();

elemType min() const;
elemType max() const;
int find(const elemType &value) const;

protected:
static const int DefaultArraySize=12;

int _size;
elemType *ia;
};

下面是cpp文件:
#include "stdafx.h"

#include <iostream>
#include "Array.h"



using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
const int array_size=4;

Array<int> ia(array_size);

Array<double> da(array_size);

Array<char> ca(array_size);

int ix;
for(ix=0;ix<array_size;++ix){
ia[ix]=ix;
da[ix]=ix*1.75;
ca[ix]=ix+'a';
}

for(ix=0;ix<array_size;++ix)
cout<<"["<<ix<<"]ia:"<<ia[ix]<<"\tca:"<<ca[ix]<<"\tda:"<<da
[ix]<<endl;

return 0;
}



...全文
275 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lengxueqingfeng 2007-03-11
  • 打赏
  • 举报
回复
他说你把模板类的声明与定义分开了,应该将类的定义也放到头文件中,或在头文件最后加上
#include file.cpp//file.cpp为你的模板类定义的文件。
有时间,可以看看这个上面别人问的问题,
像你这样的问题,这个上面就出现过几次了.
happycose 2007-03-09
  • 打赏
  • 举报
回复
不太理解呀。。。能说的明白些吗?我应该怎么改呢?谢谢
taodm 2007-03-09
  • 打赏
  • 举报
回复
google 模板的分离编译模式

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧