我的程序是如下形状:一个头文件.h,一个主函数.cpp,一个操作函数.cpp。操作函数包含头文件,然后主函数包含操作函数.cpp。运行时出现错误:
错误症状:
Linking...
xz.obj : error LNK2005: "public:
__thiscall XZ::XZ(void)" (??0XZ@@QAE@XZ) already defined in
xzmain.obj
xz.obj : error LNK2005: "public: void
__thiscall XZ::pdc(void)" (?pdc@XZ@@QAEXXZ) already defined in
xzmain.obj
xz.obj : error LNK2005: "public: void
__thiscall XZ::pcd(void)" (?pcd@XZ@@QAEXXZ) already defined in
xzmain.obj
xz.obj : error LNK2005: "public: void
__thiscall XZ::pzf(void)" (?pzf@XZ@@QAEXXZ) already defined in
xzmain.obj
xz.obj : error LNK2005: "public:
__thiscall XZ::~XZ(void)" (??1XZ@@QAE@XZ) already defined in
xzmain.obj
Debug/xzmain.exe : fatal error LNK1169:
one or more multiply defined symbols found
执行 link.exe 时出错.
解决方案一:
把主函数所包含的操作函数.cpp,改为头文件.h,然后就可以解决如上错误。
解决方案二:
重新打开编译器,重新打开刚才的文件,先打开主函数.cpp,然后编译运行,打开Fileview,其中有一个external
dependencies,把头文件.h,和操作函数.cpp添加到这里,这样再编译运行主函数.cpp,程序运行成功!
other::
1.编写MFC程序时,自定义类(未用MFC类生成向导定义的类,类定义与成员函数都放在.h头文件里),若其成员函数放在类定义外,必须成员函数定义处必须加上“inline”,即将成员函数定义为内置函数!否则,链接时就会出现“error
LNK2005:…… already defined in”错误!
此类错误解决办法:
1、将成员函数定义移入类定义内;
2、在成员函数定义加上“inline”,即将成员函数定义为内置函数
3、改用MFC类生成向导定义类
2.全局变量只能定义一次,其他文件中药用的话
应该extern 类型
变量名