C++ Android NDK Eclipse 中的 static const 多重声明错误

发布于 2024-11-10 03:30:11 字数 1186 浏览 0 评论 0原文

我读过类似的问题,但回答我的问题仅适用于 VisualStudio。我正在使用 Eclipse 并使用 Cocos2d-X 开发 Android 应用程序,Cocos2d-X 是一个使用 Android NDK 的框架。我创建了一个名为 Config 的类,其中包含应用程序的所有常量,例如球大小和 fps。下面是我如何安排代码。

Config.h

#ifndef __CONFIG_H_ // this was auto-generated by eclipse
#define __CONFIG_H_

class Config {
public:
    static const double GRAVITY;
    static const int BALL_WIDTH;
    static const int BALL_HEIGHT;
}

#endif /* config.h */

Config.cpp

#include "Config.h"


const double Config::GRAVITY = 9.8;
const int Config::BALL_WIDTH = 100;
const int Config::BALL_HEIGHT = 100;

它编译时没有错误,但是当它开始链接时,我收到以下错误:

multiple definition of `Config::GRAVITY'
C:/workspace/cocos2d-x/SampleGame/android/obj/local/armeabi/objs-debug/game/../../../Classes/Config.o:(.rodata+0xc8): first defined here
C:/workspace/cocos2d-x/SampleGame/android/obj/local/armeabi/objs-debug/game/../../../Classes/Ball.o:(.rodata+0xcc):`

所有声明的常量都会发生先前的错误。我没有将 Config.cpp 包含在任何报告的源文件的源代码中。

我不知道如何纠正这个问题。我发现了一个极其相似的问题,但答案是针对微软的VisualStudio指定的。另外,我很抱歉使用“cocos2d”标签,即使这适用于 cocos2d-X,但我希望有人知道如何解决这个问题。

I've read the similar questions, but the one that answers mine applies only to VisualStudio. I am using Eclipse and developing an Android application using Cocos2d-X, which is a framework that uses Android's NDK. I created a class named Config, which contains all of the application's constants such as ball sizes and fps. Below is how I arranged the code.

Config.h

#ifndef __CONFIG_H_ // this was auto-generated by eclipse
#define __CONFIG_H_

class Config {
public:
    static const double GRAVITY;
    static const int BALL_WIDTH;
    static const int BALL_HEIGHT;
}

#endif /* config.h */

Config.cpp

#include "Config.h"


const double Config::GRAVITY = 9.8;
const int Config::BALL_WIDTH = 100;
const int Config::BALL_HEIGHT = 100;

It compiles without errors, but when it begins linking, I get the following error:

multiple definition of `Config::GRAVITY'
C:/workspace/cocos2d-x/SampleGame/android/obj/local/armeabi/objs-debug/game/../../../Classes/Config.o:(.rodata+0xc8): first defined here
C:/workspace/cocos2d-x/SampleGame/android/obj/local/armeabi/objs-debug/game/../../../Classes/Ball.o:(.rodata+0xcc):`

The previous error occurs for all the constants declared. I have not included Config.cpp in the source code of any of the reported source files.

I have no idea how to correct this. I found an extremely similar question, but the answer was specified towards Microsoft's VisualStudio. Also, I'm sorry for using the 'cocos2d' tag, even if this applies to cocos2d-X, but I'm hoping someone knows how to fix this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

诗化ㄋ丶相逢 2024-11-17 03:30:11

发生该错误的唯一原因是您包含了 .cpp 文件。否则,您的代码完全符合标准。毕竟,该错误意味着该常量是在 Ball.o 中定义的,除非您包含 cpp,否则我发现这种情况不太可能发生。

The only way that error could occur is if you're including the .cpp file around. Else, your code is perfectly Standards-compliant. After all, the error implies that the constant was defined in Ball.o, which I find very unlikely unless you included the cpp.

亽野灬性zι浪 2024-11-17 03:30:11

就您而言,名称不匹配。您声明为 gravity,在 cpp 中它是 GRAVITY

编辑:编辑后,我发现代码中没有链接错误,除非您也在 Ball.cpp/h 文件中定义了 GRAVITY

In your case, the names doesn't match. You are declaring as gravity and in cpp it's GRAVITY.

Edit: After your edit, I see no linking errors in your code unless you have defined GRAVITY in your Ball.cpp/h file also.

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