2

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.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
cesar
  • 8,944
  • 12
  • 46
  • 59
  • It does not look like multiple definitions. It looks there is no definition. Try `static const double gravity = 9.8;` in Config.h instead of `static const double GRAVITY = 9.8;` Or is `#define GRAVITY gravity` is done somewhere? – Mayank May 26 '11 at 09:45
  • What does `::GRAVITY` have to do with `Config::gravity`? This can't possibly be your actual code... – ildjarn May 26 '11 at 09:52
  • Sorry, I wasn't paying attention, so I did not notice my sample code was wrong. Rest assured, my real code complies with all the naming conventions. I corrected my samples, and they reflect what my real code looks like. I probably wouldn't have passed the compiler check had I used the incorrect names. – cesar May 26 '11 at 10:03

2 Answers2

1

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.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • I did not include any cpp files. I only included the Config.h header file. – cesar May 26 '11 at 09:57
  • 1
    @anonymous: The error quite clearly suggests that in the "Ball.cpp" file, you defined those constants. Go look in it. – Puppy May 26 '11 at 10:12
0

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.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • They should be _initialized_ where they're declared, but they must still also be defined externally. – ildjarn May 26 '11 at 09:50
  • `static const double gravity = 9.8` won't even compile. The Standard allows in-class initialization of only `static const int` members. The definition is [still required if you use the member](http://stackoverflow.com/questions/6106194/definition-of-static-const-outside-the-class-definition/6106240#6106240). – Prasoon Saurav May 26 '11 at 09:52
  • @ildjarn, changed "defined" with "initialized". I use those interchangeably that's why that mistake. – iammilind May 26 '11 at 09:53
  • @Prasoon, `static const double gravity = 9.8;` is comipiling fine with g++ and also you can check in http://www.ideone.com/PGagW – iammilind May 26 '11 at 10:34
  • @iammilind : Comeau gives this error : `"ComeauTest.c", line 5: error: a member of type "const double" cannot have an in-class initializer static const double d = 9.8;` – Prasoon Saurav May 26 '11 at 10:36
  • 1
    @iammilind : gcc supports so many non standard extensions. I think this is also one of those. – Prasoon Saurav May 26 '11 at 10:39
  • @Prasoon, I think the term integral constant are meant indirectly for **non array POD** types. Otherwise how can it support `int, unsigned, char, double, float, enum, bool`. In your answer in the link, it doesn't state any restriction for `double` etc. – iammilind May 26 '11 at 10:44
  • 1
    @iammilind : If you think I am wrong you can create another thread for the same. AFAIK static const double members cannot be in class initialized. Non array POD types and integral constants are two completely different things... :) – Prasoon Saurav May 26 '11 at 10:47
  • @Prasoon, asking a new question will become a duplicate as something like that already exists: http://stackoverflow.com/questions/370283/why-cant-i-have-a-non-integral-static-const-member-in-a-class – iammilind May 26 '11 at 10:58
  • @iammilind : Yes so you see you can't have the initializer for a static const double member... :) – Prasoon Saurav May 26 '11 at 11:02
  • 1
    @Prasoon: . The Standard allows in-class initialization of *any* integral type members, not only `int`. All it needs to be declared as `static const integral_type`. – Nawaz May 26 '11 at 11:06
  • @Nawaz, I second you. I don't know not allowing it is a problem with Comeau. – iammilind May 26 '11 at 11:08
  • @iammilind: I think, you misunderstood my comment. `double` is not integral type. How can you agree with me when I don't agree with you?:P – Nawaz May 26 '11 at 11:10
  • @Nawaz : Yes correct! `integral_type` is what I meant in my first comment. – Prasoon Saurav May 26 '11 at 11:11
  • @Nawaz, ok then I made fool of myself :) ... I always consider "integral type" = "POD (non-array)" ... but as Prasoon says, it's wrong. – iammilind May 26 '11 at 11:12
  • @Prasoon: I had guessed that, but thought I should make it explicit in case others may misunderstand it. :-) – Nawaz May 26 '11 at 11:14