C++类重定义错误帮助

发布于 2024-09-07 08:32:49 字数 654 浏览 7 评论 0原文

我收到如下错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泅人 2024-09-14 08:32:49

也就是说整个函数被定义了两次。

我的心灵调试能力告诉我,您以某种方式递归地包含该标头,并且该标头没有适当的措施来防止这种情况的发生。因此内联函数被定义了两次。

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.

沉睡月亮 2024-09-14 08:32:49

将函数定义移动到 .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

愿得七秒忆 2024-09-14 08:32:49

如果不知道 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 and std::max in <algorithm>, and they work for all comparable types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文