我用MFC做一个工程,以对话框为基础。工程名就叫LB
里面有头文件LB.h chaxun.h guihuan.h,这3个头文件是3个对话框类的头文件。
还有list.h,这个类是我用“插入->类-新建类”这样去建立的。这4个头文件都有对应的.cpp。
LB.h把另外3个头文件都include了。问题来了,
现在我“文件-》新建-》C++ Header File”命名为"weishenme"(取为什么的意思,呵呵)。
(注意,我觉得问题就出在weishenme.h我是用上面这方式建立的?!?为什么我不在MFC底下编程,这样建立没问题?!)这样,我在weishenme.h里面,就简单地写了以下代码。注意!!weishenme.h中我没有include任何文件!!
void test1111(int aaaa)
{
int cccc;
cccc=aaaa;
};然后就出现下面的链接错误!!!我保证test1111绝对没有在list.h里面定义过!
但是假如我只写声明void test1111(int aaaa);在这个头文件中,就没有错误。
为什么!?!?!?!?救命!!
我搜索过其他贴,很多贴说这个one or more multiply defined symbols found
的问题,但我还是搞不清楚,到底是什么原因!Linking...
chaxun.obj : error LNK2005: "void __cdecl test1111(int)" (?test1111@@YAXH@Z) already defined in list.obj
guihuan.obj : error LNK2005: "void __cdecl test1111(int)" (?test1111@@YAXH@Z) already defined in list.obj
LB.obj : error LNK2005: "void __cdecl test1111(int)" (?test1111@@YAXH@Z) already defined in
Debug/LB.exe : fatal error LNK1169: one or more multiply defined symbols found
执行 link.exe 时出错.
里面有头文件LB.h chaxun.h guihuan.h,这3个头文件是3个对话框类的头文件。
还有list.h,这个类是我用“插入->类-新建类”这样去建立的。这4个头文件都有对应的.cpp。
LB.h把另外3个头文件都include了。问题来了,
现在我“文件-》新建-》C++ Header File”命名为"weishenme"(取为什么的意思,呵呵)。
(注意,我觉得问题就出在weishenme.h我是用上面这方式建立的?!?为什么我不在MFC底下编程,这样建立没问题?!)这样,我在weishenme.h里面,就简单地写了以下代码。注意!!weishenme.h中我没有include任何文件!!
void test1111(int aaaa)
{
int cccc;
cccc=aaaa;
};然后就出现下面的链接错误!!!我保证test1111绝对没有在list.h里面定义过!
但是假如我只写声明void test1111(int aaaa);在这个头文件中,就没有错误。
为什么!?!?!?!?救命!!
我搜索过其他贴,很多贴说这个one or more multiply defined symbols found
的问题,但我还是搞不清楚,到底是什么原因!Linking...
chaxun.obj : error LNK2005: "void __cdecl test1111(int)" (?test1111@@YAXH@Z) already defined in list.obj
guihuan.obj : error LNK2005: "void __cdecl test1111(int)" (?test1111@@YAXH@Z) already defined in list.obj
LB.obj : error LNK2005: "void __cdecl test1111(int)" (?test1111@@YAXH@Z) already defined in
Debug/LB.exe : fatal error LNK1169: one or more multiply defined symbols found
执行 link.exe 时出错.
然后直接现在“文件-》新建-》C++ Header File”命名为"weishenme"
这样,我在weishenme.h里面,就简单地写了以下代码。注意!!weishenme.h中我没有include任何文件!!
void test1111(int aaaa)
{
int cccc;
cccc=aaaa;
};
然后在ttx.h中#include weishenme.h
ttxDlg.obj : error LNK2005: "void __cdecl test111(int)" (?test111@@YAXH@Z) already defined in ttx.obj这个是为什么?为什么平时不在MFC基础下,这样新建.h文件没有问题,到MFC,这样就有问题?
应该怎么解决?
我把2楼的那个情况,把实现放在weishenme.cpp中,weishenme.h中只声明。
这样问题解决了。但是在我楼顶的那个情况下,如果weishenme.h不是什么都不include.而是了一个.h
这样,我把声明和实现分离的情况下,还是会出现同样的链接错误哦!!
void test1111(int aaaa)
{
int cccc;
cccc=aaaa;
};
所有会出现函数重复定义的错误。
#ifndef WEISHENME_H_
#define WEISHENME_H_void test1111(int aaaa)
{
int cccc;
cccc=aaaa;
}; #endif
----------------------
关键不在于include了什么文件,而是在于weishenme.h被两个以上文件include了。
#pragma once
头文件重复包含了,所以会出现这个问题。#pragma once的意思是只编译一次,这样应该就没问题了。05以上的版本应该会自动加的吧。
void test1111(int aaaa)
{
int cccc;
cccc=aaaa;
};
所以编译时就出现了重定义的错误
#define WEISHENME_H_这样可以防止头文件被重复引用,也就不会出现那样的问题。
所谓引用,你可以将其理解为:源文件组合。呵呵