读书人

重复定义的异常

发布时间: 2013-01-02 13:08:45 作者: rapoo

重复定义的错误
建了一个win32控制台工程,里面只有LinkFunSeries.cpp和LinkMain.cpp两个源文件,LinkFunSeries.cpp里是各个链表处理函数的定义,LinkMain.cpp是对链表处理函数简单调用以验证其正确性。
LinkFunSeries.cpp内容大概如下:
#include <iostream>
using namespace std;
#ifndef LinkFunSeries_V_M
#define LinkFunSeries_V_M
struct student{int num;float score;struct student *next;};
.......
#endif

LinkMain.cpp内容大概如下:
#include <iostream>
#include "LinkFunSeries.cpp"
using namespace std;
void main()
{
......
}

这两个文件各自进行单独编译时都没错,但连接编译时就报错了,错误信息如下:
Linking...
LinkFunSeries.obj : error LNK2005: "void __cdecl DelSort(struct student *,struct student *)" (?DelSort@@YAXPAUstudent@@0@Z) already defined in LinkMain.obj
LinkFunSeries.obj : error LNK2005: "void __cdecl DelOne(struct student *,struct student *)" (?DelOne@@YAXPAUstudent@@0@Z) already defined in LinkMain.obj
.......
说那些函数在LinkMain里重复定义了,但LinkMain里根本就没定义过任何函数,求解???!!!
二楼放上完整程序。
[解决办法]
初学者的确常常弄混头文件和源文件的区别

简单地说,源文件是放源代码的地方,头文件是放几个源文件都要写的一模一样的源代码的地方,include就是一个简单的复制命令
源文件编译成模块,各模块链接成可执行程序或链接库,至于头文件,预编译的时候它里面的代码就被复制到源文件中然后结束其使命
也就是说,源文件是本质,头文件是个附加的小工具,虽然这个工具很好用,但必须牢记它只是个工具而已
[解决办法]
#include "LinkFunSeries.cpp" ??????????? 为何不是.h ???

读书人网 >C++

热点推荐