悄悄地问下,这个是什么错误。

oosky2004 2005-09-01 10:46:49
悄悄地问下,这个是什么错误。编译单个文件cpp文件的时候没有错。

MyAllExe.obj : error LNK2001: unresolved external symbol "public: __thiscall Array<char>::~Array<char>(void)" (??1?$Array@D@@QAE@XZ)
MyAllExe.obj : error LNK2001: unresolved external symbol "public: char const * __thiscall Array<char>::GetData(void)const " (?GetData@?$Array@D@@QBEPBDXZ)
MyAllExe.obj : error LNK2001: unresolved external symbol "public: __thiscall Array<char>::Array<char>(unsigned int,unsigned int)" (??0?$Array@D@@QAE@II@Z)
...全文
174 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
oosky2004 2005-09-01
  • 打赏
  • 举报
回复
谢谢,楼上的。
为什么,声明和定义分开了,就不行呢?
xiaocai0001 2005-09-01
  • 打赏
  • 举报
回复
将实现部分通通移到定义文件中去就可以了~
oosky2004 2005-09-01
  • 打赏
  • 举报
回复
int main(int argc, char* argv[])
{

cout<<"-------------the below is Array Class action ------------"<<endl;
Array <int> myarray(10, 0);
cout<<myarray.GetData();



return 0;
}
compile main.cpp 没有错误。
build main.cpp就出现以上错误了。
mituzhishi 2005-09-01
  • 打赏
  • 举报
回复
不懂,关注。
oosky2004 2005-09-01
  • 打赏
  • 举报
回复
实现部分也帖出来。
#include "StdAfx.h"
#include "Array.hpp"


/* -------------------------------------------------------------------
Array<T>::Array ()
------------------------------------------------------------------- */
//template <class T>
//Array<T>::Array (unsigned int n) :
// data(new T [n]),
// base (0),
// length (0)
//{ }

/* -------------------------------------------------------------------
Array<T>::Array(unsigned int n, unsigned int m):
------------------------------------------------------------------- */
template <class T>
Array<T>::Array(unsigned int n, unsigned int m) :
data (new T [n]), base (m), length(n)
{ }

/* -------------------------------------------------------------------
Copy construct function
------------------------------------------------------------------- */
template <class T>
Array<T>::Array(Array const& array) :
data (new T [array.length]), base (array.base), length (array.length)
{
for(unsigned int i = 0; i < length; ++i)
data[i] = array.data[i];
}

/* -------------------------------------------------------------------
destructor
------------------------------------------------------------------- */
template <class T>
Array<T>::~Array() {

delete []data;
}

/* -------------------------------------------------------------------
Array<T>::Data()
------------------------------------------------------------------- */
template <class T >
T const* Array<T>::GetData() const {

return data;
}

/* -------------------------------------------------------------------
Array<T>::Base()
------------------------------------------------------------------- */
template < class T >
unsigned int Array<T>::GetBase() const {

return base;

}

/* -------------------------------------------------------------------
Array<T>::Length()
------------------------------------------------------------------- */
template <class T>
unsigned int Array<T>::GetLength() const {

return length;
}

/* -------------------------------------------------------------------
T const & Array<T>::operator []
------------------------------------------------------------------- */
template <class T >
T const & Array<T>::operator [] (unsigned int position) const{

unsigned int const offset = position - base;
if(offset >= length)
throw out_of_range ("invalid position.");
return data[offset];
}

/* -------------------------------------------------------------------
T& Array<T>::operator []
------------------------------------------------------------------- */
template <class T >
T& Array<T>::operator [](unsigned int position){

unsigned int const offset = position - base;
if (offset >= length) {
throw out_of_range("invalid position.");
}
return data [offset];
}

/* -------------------------------------------------------------------
Array<T>::SetBase(unsigned int newbase)
------------------------------------------------------------------- */
template <class T >
void Array<T>::SetBase(unsigned int newbase){

base = newbase;
}

/* -------------------------------------------------------------------
void Array<T>::SetLength(unsigned int newlength)
------------------------------------------------------------------- */
template<class T >
void Array<T>::SetLength(unsigned int newlength){

T* const newdata = new T (newlength);
unsigned int const min = length < newlength ? length : newlength;
for (unsigned int i = 0; i< min; ++i) {
newdata[i] = data[i];
}
delete [] data;
data = newdata;
length = newlength;
}
oosky2004 2005-09-01
  • 打赏
  • 举报
回复
这个是声明部分:
#if !defined (_ARRAY_H)
#define _ARRAY_H


template <class T>
class Array
{
protected:
T * data;
unsigned int base;
unsigned int length;
public:
/*Array (unsigned int);*/
Array (unsigned int, unsigned int =0);
Array (Array const&);
~Array ();


Array& operator = (Array const&);
T const& operator [] (unsigned int) const;
T& operator [] (unsigned int);

T const* GetData () const;
unsigned int GetBase () const;
unsigned int GetLength () const;

void SetBase (unsigned int);
void SetLength (unsigned int);

};

#endif
mituzhishi 2005-09-01
  • 打赏
  • 举报
回复
有两个函数声明了但是没有给出实现

两个函数是 getData() 和 Array()
superwyf 2005-09-01
  • 打赏
  • 举报
回复
是不是,有派生类没有实现基类的纯虚函数?
GetData(void)const
mituzhishi 2005-09-01
  • 打赏
  • 举报
回复
有两个函数给出了声明,但是找不到实现。

两个函数分别是getData() 和 Array()
曾永红_ 2005-09-01
  • 打赏
  • 举报
回复
可能是宽窄字符设置问题
曾永红_ 2005-09-01
  • 打赏
  • 举报
回复
找不到链接
oosky2004 2005-09-01
  • 打赏
  • 举报
回复
刚刚用下面两个编译器试试了。
dev-c++,
eclipse
好像都不支持模板类的实现和定义分开写。

对eclipse 不是很熟悉。
OMA_yudy 2005-09-01
  • 打赏
  • 举报
回复
用Dev-C++试试。
oosky2004 2005-09-01
  • 打赏
  • 举报
回复
谢谢。明白了。
xiaocai0001 2005-09-01
  • 打赏
  • 举报
回复
你用的是VC吧
在VC里面,一涉及到模板或友元等东西,编译器就是怪怪的

写模板类的时候,声明与实现必须放一起,否则编译器不认.~
好怪哦~~
mituzhishi 2005-09-01
  • 打赏
  • 举报
回复
同意楼上,那样就不用链接了,自然不会出现链接错误。

64,654

社区成员

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

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