C++多文件编译问题
错误如下:
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 个 ==========
代码如下:
////////////////////////////////////////////////////
////////////////////////////////////////////////////////
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;
}
...全文
相关推荐
Dev-C++ 编译多文件程序的方法&安装使用教程
Dev-C++ 编译多文件程序的方法在使用 Dev-C++ 编译多文件程序时,开发者可能会遇到一个常见的问题:程序运行结束后,窗口一闪而过的问题。这并不是程序的问题,而是因为程序运行结束,自动关闭了。
Dev-Cpp C++编译器
标题中的“Dev-Cpp C++编译器”指的是这个IDE的核心功能,即它内置了C++编译器,使得用户能够直接在该环境中创建和管理C++项目。
实验一C++程序在Visual-C++6.0编译系统中的实现 (2).docx
在多文件程序中,f1.cpp通常包含主函数main(),而f2.cpp包含其他函数(如add())。在编译和连接多文件程序时,Visual C++6.0会处理所有相关文件,确保正确链接各个函数。
triangle多文件
在C++中,静态库是一种将代码编译到目标代码中的方式,使得最终的可执行文件包含了库的所有代码。使用静态库的优点包括避免依赖问题和减少运行时的内存占用。
在Linux下编译并运行C程序
综上所述,本篇文章涵盖了在Linux环境下编译和运行C/C++程序的基础知识,包括环境配置、简单程序的编译、多文件编译以及使用Makefile简化编译流程的方法。


