symbol not found

Whenever I run this code, I get this error:

Undefined symbols for architecture x86_64:
"Producer::lifespan", referenced from:
Producer::init_producer(std::string, int, int) in Producer.o
"Producer::birth_rate", referenced from:
Producer::init_producer(std::string, int, int) in Producer.o
"Producer::name", referenced from:
Producer::init_producer(std::string, int, int) in Producer.o
"Consumer::lifespan", referenced from:
Consumer::init_consumer(int, std::string, int, int) in Consumer.o
"Consumer::birth_rate", referenced from:
Consumer::init_consumer(int, std::string, int, int) in Consumer.o
"Consumer::name", referenced from:
Consumer::init_consumer(int, std::string, int, int) in Consumer.o
"Consumer::level", referenced from:
Consumer::init_consumer(int, std::string, int, int) in Consumer.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Consumer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #ifndef Ecological_Mapping_Consumer_h
#define Ecological_Mapping_Consumer_h

#include <string>

using namespace std;

class Consumer
{
public:
    
    static int level;
    static string name;
    static int birth_rate;
    static int lifespan;
    
    static void init_consumer(int lv, string nm, int br, int ls);
    static void remv_consumer();
};

#endif


Consumer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "Consumer.h"

void Consumer::init_consumer(int lv, string nm, int br, int ls)
{
    Consumer::level = lv;
    Consumer::name = nm;
    Consumer::birth_rate = br;
    Consumer::lifespan = ls;
}
void Consumer::remv_consumer()
{
    
}


Producer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#ifndef Ecological_Mapping_Producer_h
#define Ecological_Mapping_Producer_h

#include <string>

using namespace std;

class Producer 
{
public: 
    
    static string name;
    static int birth_rate;
    static int lifespan;
    
    static void init_producer(string nm, int br, int ls);
    static void remv_producer();
};

#endif


Producer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include <iostream>
#include "Producer.h"

void Producer::init_producer(string nm, int br, int ls)
{
    Producer::name = nm;
    Producer::birth_rate = br;
    Producer::lifespan = ls;
}
void Producer::remv_producer()
{
    
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>

#include "Producer.h"
#include "Consumer.h"


using namespace std;

string s_temp_1;
string s_temp_2;
int i_temp_1;
int i_temp_2;
int i_temp_3;
int i_temp_4;

int main ()
{
    cout << "ECOLOGICAL MAPPING:" << endl;
    cout << "Name of Organsim:" << endl;
    cin >> s_temp_1;
    cout << "Producer or Consumer:" << endl;
    cin >> s_temp_2;
    cout << "Birth Rate of Organsim (Per Year):" << endl;
    cin >> i_temp_1;
    cout << "Average Lifespan of Organsim (In Years):" << endl;
    cin >> i_temp_2;
    if(s_temp_2 != "producer" || s_temp_2 != "Producer" || s_temp_2 != "p" || s_temp_2 != "P" )
    {
        cout << "Primary, Secondary, Tertiary, or Quarternary: (1,2,3,4)" << endl;
        cin >> i_temp_3;
        Consumer::init_consumer(i_temp_3, s_temp_1, i_temp_1, i_temp_2);
    }
    else
    {
        
    }
}

i've done projects like this before, but I don't know what i'm doing wrong. Any help appreciated. Thanks!
Because static members area shared among objects, they are not defined for each object. You have to define them in a separate place, most commonly the source file where you also define the methods. So for example in your Consumer.cpp file, you would have:
1
2
3
4
5
6
#include "Consumer.h"

int Consumer::level;
std::string Consumer::name;
int Consumer::birth_rate;
int Consumer::lifespan;

Note that you do not include static when defining static members.

By the way, if you have a class whose members are all public and static... You may as well just use a namespace.
Topic archived. No new replies allowed.