奇怪的标头多重定义错误
Strange multiple definitions error with headers
我在我的项目中有一个奇怪的多个定义错误。我使用#ifndef
预处理器命令来避免多次包含相同的文件。我清除了所有其他代码。以下是我的简化文件:
1 - main.cpp
#include "IP.hpp"
int main()
{
return 0;
}
2 - IP.cpp
#include "IP.hpp"
//some codes!
3 - IP.hpp
#ifndef IP_HPP_INCLUDED
#define IP_HPP_INCLUDED
unsigned char LUTColor[2];
#endif // IP_HPP_INCLUDED
使用代码块&Gnu GCC在win7中,它说:
在我删除所有其他代码之前,错误是:objDebugmain.o:C:UsersaaaDocumentsprgct3main.cpp|4|这里首次定义|
||===构建完成:1个错误,0个警告===|
||=== edgetest, Debug ===|
obj 调试 IP。0 ||In function ' Z9getHSVLUTPA256_A256_12colorSpace3b':|
c:program filescodeblocksmingwbin..libgccmingw324.4.1includec++exception|62|多重定义' LUTColor'|
objDebugmain.o:C:UsersaaaDocumentsprgedgetestmain.cpp|31|这里首次定义|
||===构建完成:2个错误,0个警告===|
和'LUTColor'在IP.hpp !
怎么了?
问题在标题-你需要:
#ifndef IP_HPP_INCLUDED
#define IP_HPP_INCLUDED
extern unsigned char LUTColor[2]; // Declare the variable
#endif // IP_HPP_INCLUDED
- 不要在头文件中定义变量!
您还需要指定一个源文件来定义LUTColor
(IP.cpp是明显的位置)。
参见:什么是C中的外部变量,其中大部分适用于c++和C。
相关文章:
- 使用命名空间时出现多个定义错误
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 链接阶段出现多重定义错误
- 避免模板类中的重定义错误
- 即使我没有包含多个文件,C++中的多个定义错误
- 跨多个类的全局变量而不会出现重定义错误?
- 尝试运行 wasm 函数时出现模块未定义错误
- C++ 预期的左大括号以及重定义错误
- C++:成员的越界声明必须是纯虚函数的定义错误
- c++中数组的未定义错误
- Visual C:模板类中的自定义错误消息
- 为什么C++模板不会导致多个定义错误?
- 只有一个定义/声明时标头声明变量的多堆定义错误
- C++ 在多个其他类中使用单个类 - 编译时出现多个定义错误
- 基类未定义.错误 C2504
- Q 斯坦达项重定义错误
- 可视化C++中的结构定义错误
- VC++ C2011 重定义错误 - 未使用的头文件