2

I am trying to make a warhammer 40K army counter program so I don't have to keep using excel. My problem is that my program gets an error because I have two different objects and their header files with the same variable names.

When I am trying to run my program g++ complains that the variable has already been declared in my first object.

How do I make it so that this won't happen?

Here is a code snippit of SKulltaker.cpp program

#include "SkullTaker.h"
#include <string>
int pointCost = 140;
int minSize = 1;
int maxSize = 1;
std::vector<std::string> rules;

Here is the SkullTaker.h

class SkullTaker {
public:

    SkullTaker();
    SkullTaker(const SkullTaker& orig);
    int getPointCost();
    int getMinSize();
    int getMaxSize();
    std::vector<std::string> getRules();
    std::string toString();
    virtual ~SkullTaker();
};

My other class is the same but the name of the .cpp and .h file in KuGath.

g++ complaining that there are multiple definitions of PointCost.

Thanks,

dhoehna

1
  • Do you have include guards in your header file? Commented Aug 16, 2012 at 22:22

4 Answers 4

3

use "static" keyword to indicate that your global variable's scope should be limited to it's translation unit only.

static int pointCost = 140;

Sign up to request clarification or add additional context in comments.

1 Comment

That works. Thank you very much. When I am not so tired I'm gonna look up the static keyword and see everything it does.
2

Use an 'anonymous' namespace:

namespace {
int pointCost = 140;
int minSize = 1;
int maxSize = 1;
std::vector<std::string> rules;
}
//the code that uses these comes here.

Everything within that will only be visible within that file. Another option - if you only need those for the class definition - would be to include these as static private members of your class.

class Skulltaker {
    public:
    ...
    private: /* I believe this initialization syntax is allowed in C++11
                otherwise, initialize them in your .cpp file
                like this: int Skulltaker::pointCost(140);*/
    static int pointCost = 140;
    static int minSize = 1;
    static int maxSize = 1;
    static std::vector<std::string> rules;
};

Well, but think about it like that - now you have a first hand experience as to why people keep telling you to NOT use globals if at all possible (aside from design issues).

Comments

2

You are making your pointcost global, and not belonging to a namespace. You could make it a member of your class (private: int pointcost;), and then in your cpp in the constructor or whatever set pointcost=140. Since this is actually something which looks like every class could be derived from a base class called "WarHammerUnit", it could be a member there instead, and you set it for each unit in your constructor.

Comments

0

If that is the full file you need a using namespace std; command after the includes. Given you're having a namespace problem that might fix it... There should be no conflict between variable names in your classes if all of your compiler directives are good (includes, ifdefs, ect).

Also the classes properties should be declared in the header file, it looks like you have those two mixed up (or maybe it's just a mistake in writing the question). If those values are supposed to be constant you can declare them as static const in the header file and initialize them there, if they'll vary from instance to instance you should be initializing them in the constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.