gpt4 book ai didi

c++ - 带有标题的奇怪的多重定义错误

转载 作者:行者123 更新时间:2023-11-28 01:01:07 25 4
gpt4 key购买 nike

我的项目中有一个奇怪的多重定义错误。我正在使用 #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

在 win7 中使用 codeblocks & gnu gcc,它说:

obj\Debug\main.o:C:\Users\aaa\Documents\prg\ct3\main.cpp|4|first defined here|

||=== Build finished: 1 errors, 0 warnings ===|

在我删除所有其他代码之前,错误是:

||=== edgetest, Debug ===|

obj\Debug\IP.o||In function `Z9getHSVLUTPA256_A256_12colorSpace3b':|

c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\exception|62|multiple definition of `LUTColor'|

obj\Debug\main.o:C:\Users\aaa\Documents\prg\edgetest\main.cpp|31|first defined here|

||=== Build finished: 2 errors, 0 warnings ===|

'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 是显而易见的地方)。

另请参阅:What are extern variables in C ,其中大部分适用于 C++ 和 C。

关于c++ - 带有标题的奇怪的多重定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8634096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com