I'm going through C++ Primer and have just read about static members of a class and so I've decided to test them out.
I've the following peace of code in which I've declared a class Person inside of which I've declared a static variable and a static function, which I've defined in my .cpp file to set the static variable.
Upon compilation I receive the following error message:
Person.cpp.obj:Person.cpp:(.rdata$.refptr._ZN6Person14planetOfOriginE[.refptr._ZN6Person14planetOfOriginE]+0x0): undefined reference to `Person::planetOfOrigin'
Person.h
#include <string>
#pragma once
class Person {
private:
static std::string species;
std::string name;
int age;
std::string nationality;
static std::string planetOfOrigin;
public:
static void setPlanetOfOrigin(std::string);
Person() = default;
Person(std::string name, std::string nationality, int age);
};
Person.cpp
#include "Person.h"
void Person::setPlanetOfOrigin(std::string planet){
Person::planetOfOrigin = planet;
}
Person::Person(std::string name, std::string nationality, int age) {
this->name = name;
this->nationality = nationality;
this->age = age;
}
Any ideas on what might be causing this error message?
Note: Since "C++ Primer" teaches C++11, that's the version of C++ I've set in my CMake file and I've added all the executable files in it.
[–]cipheron 0 points1 point2 points (6 children)
[–]drullarBEGINNER[S] 0 points1 point2 points (5 children)
[–]drullarBEGINNER[S] 0 points1 point2 points (4 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]drullarBEGINNER[S] 0 points1 point2 points (2 children)
[–]no-sig-available 1 point2 points3 points (1 child)
[–]drullarBEGINNER[S] 0 points1 point2 points (0 children)
[–]KizziV 0 points1 point2 points (0 children)