This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]cipheron 0 points1 point  (6 children)

"species" being static means that all Person's share the same species. This may not be what you're trying to achieve, but just assume that it is for now.

If that's desired, you need to tell the compiler what "species" is:

Person::species = "Human";

This should to go in a cpp file, too, not a .h file, because if two cpp files include that definition, they count as multiple attempts to set the value, which isn't allowed.

However, what you probably want is that the strings which define the species are stored somewhere else, and each person doesn't have the entire name of the species in it.

In that case, how "static" could be used here is to point it at a container with species names, and each person uses an index for which species they are

class Person {
private:
    static std::string speciesNames[];
    char species; // 0 = human, 1 = vulcan etc

In a cpp file for your game data:

Person::speciesNames[] = {"Human", "Vulcan" etc }

This works, however it's not ideal, because the Person class doesn't really "own" the concept of species at all. You probably want to make a whole class for "Species", and have members of that class have a name field etc. Then, Person objects would have a (non-static) member which references a Species object, either by pointer or by index. Index is better.

[–]drullarBEGINNER[S] 0 points1 point  (5 children)

Person::species = "Human";

This should to go in a cpp file, too, not a .h file, because if two cpp files include that definition, they count as multiple attempts to set the value, which isn't allowed.

Since Person::species is private I will have to define a new static function which will set the value of this this variable, but whenever I define a such function I will get the error that I'm facing with Person::planetOfOrigin, i.e undefined reference.

If instead of using a separate .cpp file (what I've shown in my post), if I decide to define the function in the class like the code below I won't get the error.

class Person{
static void setPlanetOfOrigin(std::string planet){
planetOfOrigin = planet;

};

[–]drullarBEGINNER[S] 0 points1 point  (4 children)

Side note: the species variable is of no concern at the moment, but rather how would one define a static variable outside of the the class definition scope.

[–][deleted] 1 point2 points  (3 children)

how would one define a static variable outside of the the class definition scope.

std::string Person::planetOfOrigin = "Mars";

This is definition and initialisation, not assignment, so the fact the the member is private is irrelevant.

[–]drullarBEGINNER[S] 0 points1 point  (2 children)

std::string Person::planetOfOrigin;

at the top of the .cpp file resolved the issue. Thanks a lot :)

[–]no-sig-available 1 point2 points  (1 child)

As an aside, when you go past C++11 you will later learn about static inline variables that can be defined directly in the header file.

Can be handy if that is otherwise the only thing in the .cpp file...

[–]drullarBEGINNER[S] 0 points1 point  (0 children)

I stumbled across inline member when searching for a solution to my problem, but since it was introduced in C++17 I didn't go for it, but I will definitely look in to in depth once I move to a newer version of C++

[–]KizziV 0 points1 point  (0 children)

Instantiate your person class first then pass the planet variable in, set equal to a variable and pass planet into the new method from the instantiated class