C++类重定义错误帮助
我收到如下错误:
FxMathFunctions.h: In function 'FxInt32 IMin(FxInt32, FxInt32)':
FxMathFunctions.h:13: error: redefinition of 'FxInt32 IMin(FxInt32, FxInt32)'
FxMathFunctions.h:15: error: 'FxInt32 IMin(FxInt32, FxInt32)' previously defined here
在 FxMathFunctions.h 中,我有:
11: struct FxPoint2d;
12:
13: inline FxInt32 IMin(FxInt32 i1,FxInt32 i2)
14: {
15: if (i2 < i1) i1 = i2;
16: return i1;
17: }
FxInt32 在标头中定义,我将其包含为:
typedef long FxInt32;
我无法通过错误来决定是否正在重新定义 FxInt32 或者是否正在重新定义整个函数。
我该如何解决这个问题?
更新我在上面添加了行号。
I am getting errors like:
FxMathFunctions.h: In function 'FxInt32 IMin(FxInt32, FxInt32)':
FxMathFunctions.h:13: error: redefinition of 'FxInt32 IMin(FxInt32, FxInt32)'
FxMathFunctions.h:15: error: 'FxInt32 IMin(FxInt32, FxInt32)' previously defined here
In FxMathFunctions.h I have:
11: struct FxPoint2d;
12:
13: inline FxInt32 IMin(FxInt32 i1,FxInt32 i2)
14: {
15: if (i2 < i1) i1 = i2;
16: return i1;
17: }
FxInt32 is defined in a header that I am including as:
typedef long FxInt32;
I cant decide by the errors if it says that FxInt32 is being redefined or if the whole function is.
How do I solve this?
UPDATE I added the line numbers above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也就是说整个函数被定义了两次。
我的心灵调试能力告诉我,您以某种方式递归地包含该标头,并且该标头没有适当的措施来防止这种情况的发生。因此内联函数被定义了两次。
It's saying the whole function is defined twice.
My psychic debugging powers tell me you are somehow recursively including that header, and that header doesn't have a proper guard against this happening. Thus the inline function is defined twice.
将函数定义移动到 .cpp 文件中,然后就可以了
.h 文件中的原型。让编译器操心优化
Move the function definition into a .cpp file and just have
the prototype in the .h file. Let the compiler worry about the optimization
如果不知道 FxMathFunctions.h 第 13 行和第 15 行的内容,就很难说清楚。也就是说,请记住 C++ 有一个内置的
std::min< /code>
和
std::max
在
中,并且它们适用于所有可比较的类型。It's hard to say without knowing what's on lines 13 and 15 of FxMathFunctions.h. That said, keep in mind that C++ has a built-in
std::min
andstd::max
in<algorithm>
, and they work for all comparable types.