C++多文件编译问题

wyn990 2009-04-06 09:44:33
错误如下:
1>正在编译...
1>myArrayTestMain.cpp
1>正在链接...
1>LINK : 没有找到 E:\学习相关\VC_works\ArrayClass\Debug\ArrayClass.exe 或上一个增量链接没有生成它;正在执行完全链接
1>myArrayTestMain.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall MyArray<int>::~MyArray<int>(void)" (??1?$MyArray@H@@QAE@XZ),该符号在函数 _main 中被引用
1>myArrayTestMain.obj : error LNK2019: 无法解析的外部符号 "public: int __thiscall MyArray<int>::ListSize(void)const " (?ListSize@?$MyArray@H@@QBEHXZ),该符号在函数 _main 中被引用
1>myArrayTestMain.obj : error LNK2019: 无法解析的外部符号 "public: int & __thiscall MyArray<int>::operator[](int)" (??A?$MyArray@H@@QAEAAHH@Z),该符号在函数 _main 中被引用
1>myArrayTestMain.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall MyArray<int>::MyArray<int>(int)" (??0?$MyArray@H@@QAE@H@Z),该符号在函数 _main 中被引用
1>E:\学习相关\VC_works\ArrayClass\Debug\ArrayClass.exe : fatal error LNK1120: 4 个无法解析的外部命令
1>生成日志保存在“file://e:\学习相关\VC_works\ArrayClass\ArrayClass\Debug\BuildLog.htm”
1>ArrayClass - 5 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========


代码如下:
////////////////////////////////////////////////////

//myArrayTestMain.cpp
#include"myArray.h"
//#include"MyArray.cpp"

int main()
{
MyArray<int> myArray_1(10);
myArray_1[3] = 100;
cout<<"数组大小:"<<myArray_1.ListSize();
cout<<"\nmyArray_1[3] = "<<myArray_1[3]<<endl;
return 0;
}

////////////////////////////////////////////////////////
//myArray.h
#ifndef MYARRAY_H
#define MYARRAY_H
#include<iostream>
using namespace std;

#ifndef NULL
const int NULL=0;
#endif //NULL

//define the error types
enum ErrorType
{invalidArraySize,memoryAllocationError,indexOutOfRange};

//declare the MyArray template
template<class T>
class MyArray
{
private:
T * alist; //store the head address
int size;
void Error(ErrorType error,int badIndex=0) const;
public:
MyArray(int sz=50);
MyArray(const MyArray<T>& A);
~MyArray(void);
MyArray<T> & operator=(const MyArray<T> & rhs);
T & operator[](int i);
operator T* (void) const;//MyArray类对象名转换成T类型的指针
int ListSize(void) const;
void ReSize(int sz);
};
#endif
///////////////////////////////////////////////////////////////////
//MyArray.cpp


//#include<iostream>
//#include<cstdlib>
#include"myArray.h"
//using namespace std;


//输出错误信息
//error messages



template<class T>
void MyArray<T>::Error(ErrorType error,int badIndex) const
{
#ifndef ERRORMSG
#define ERRORMSG
char *errorMsg[]={"Invalid array size","Memory allocation error","Invalid index:"};
#endif
cout<<errorMsg[error];
if(error==indexOutOfRange)
cout<<badIndex;
cout<<endl;
exit(1);
}


//constructing function
template<class T>
MyArray<T>::MyArray(int sz)
{
if(sz<=0)
Error(invalidArraySize);
size=sz;
alist = new T[size];
if(alist == NULL)
Error(memoryAllocationError);
}

//destructing function
template<class T>
MyArray<T>::~MyArray(void)
{ delete []alist; }


//copy constructing function
template <class T>
MyArray<T>::MyArray(const MyArray<T>& X)
{
int n = X.size;
size = n;
alist = new T[n];
if(alist == NULL)
Error(memoryAllocationError);
T* srcptr = X.alist;
T* destptr = alist;
while(n--)
*destptr++ = *srcptr++;
}

//重载 '=' 操作符
template <class T>
MyArray<T> & MyArray<T>::operator= (const MyArray<T> & rhs)
{
int n = rhs.size;
if(size!=n)
{
delete [] alist;
alist = new T[n];
if(alist == NULL)
Error(memoryAllocationError);
size = n;
}
//开始复制元素
T * destptr = alist;
T * srcptr = rhs.alist;
while(n--)
*destptr++ = *srcptr++;
return * this;
}


//重载下标运算符
template <class T>
T & MyArray<T>::operator[] (int n)
{
if(n < 0||n > size -1)
Error(indexOutOfRange,n);
return alist[n];
}

//重载指针运转换算符
template <class T>
MyArray<T> ::operator T*(void) const
{
return alist;
}

//取当前数组的大小
template <class T>
int MyArray<T>::ListSize(void) const
{
return size;
}

//将数组大小修改为sz
template <class T>
void MyArray<T>::ReSize(int sz)
{
if(sz <= 0)
Error(invalidArraySize);
if(sz == size)
return;
T * newlist = new T[sz];
if(newlist == NULL)
Error(memoryAllocationError);
int n = (sz <=size)?sz : size;
T* srcptr = alist;
T* destptr = newlist;
while(n--)
*destptr++ = *srcptr++;
delete [] alist;
alist = newlist;
size = sz;
}
...全文
181 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jakqigle 2009-04-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wuyu637 的回复:]
模板的声明和实现必须在同一个文件里。


把模板的声明和实现方在同一个头文件中,在包含main函数的文件中包含这个模板的头文件。

===============================================
[/Quote]
这是必须的吗,那要是不想把实现对外公布怎么办呢?
wyn990 2009-04-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wuyu637 的回复:]
模板的声明和实现必须在同一个文件里。


把模板的声明和实现方在同一个头文件中,在包含main函数的文件中包含这个模板的头文件。

===============================================
[/Quote]
小弟是菜鸟啊,呵呵~~ 这个是c++的规定么?
gxw145 2009-04-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wuyu637 的回复:]
模板的声明和实现必须在同一个文件里。


把模板的声明和实现方在同一个头文件中,在包含main函数的文件中包含这个模板的头文件。

===============================================
[/Quote]

template<class T>
class MyArray
{
private:
T * alist; //store the head address
int size;
void Error(ErrorType error,int badIndex=0) const;
public:
MyArray(int sz=50);
MyArray(const MyArray<T>& A);
~MyArray(void);
MyArray<T> & operator=(const MyArray<T> & rhs);
T & operator[](int i);
operator T* (void) const;//MyArray类对象名转换成T类型的指针
int ListSize(void) const;
void ReSize(int sz);
};
这个类的实现放同一头文件里
wuyu637 2009-04-06
  • 打赏
  • 举报
回复
模板的声明和实现必须在同一个文件里。


把模板的声明和实现方在同一个头文件中,在包含main函数的文件中包含这个模板的头文件。

===============================================
jakqigle 2009-04-06
  • 打赏
  • 举报
回复
.h文件中不要用 using...
ayw215 2009-04-06
  • 打赏
  • 举报
回复
同意ls各位
如若还不行,把模板实现的那个cpp合并到h文件里
alan001 2009-04-06
  • 打赏
  • 举报
回复
在工程里添加文件
morris88 2009-04-06
  • 打赏
  • 举报
回复
貌似没有将MyArray.cpp加进工程
se7venhigh 2009-04-06
  • 打赏
  • 举报
回复
之前也碰到过类似问题,百度一下结果是:出现错误的函数定义不成功.
观望之,高手来解答
Jarrys 2009-04-06
  • 打赏
  • 举报
回复
.h中不能用using 吧, 好像是 ?????

你自己试试去。。。
alexhilton 2009-04-06
  • 打赏
  • 举报
回复
.h中不能用using 吧, 好像是
Jarrys 2009-04-06
  • 打赏
  • 举报
回复
也可以不放在.h文件中,把文件导出,可以使用export关键字
把实现代中码放在头文件中,编译器必须在每个包含该头文件的源文件中处理这些代码,这会大大增加处理的系统开销:
exprot template<typename T>
void MyArray<T>::ReSize(int sz)
{
....//实现代码
}

64,678

社区成员

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

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