Problem with Class constructor

Hello Everybody . I'm new to the C++ community and environment too.
Anyway , I was trying to code a tiny portion of a RPG game in console and the problem comes with my Character Class.
The compiler refuse to compile because of that error: undefined reference to 'Character::Character()'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Main.cpp

#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <time.h>
#include "Character.h"


using namespace std;


int main()
{
    srand(time(0));
    Character junior, frank;
    return 0;
}

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56



//Character.cpp

#include "Character.h"


using namespace std;

Character::Character()
{
    m_vie = 100;
    m_mana = 100;
}

    void Character::showlife ()
    {

    }

    void Character:: getdmg (int m_vie)
    {
       m_vie -= 10;
    }

    void Character::attack (Character &target)
    {

    }

    void Character::healing (int m_vie)
    {
      m_vie += 20;
    }

    void Character::manaregen (int m_mana)
    {

    }

    void Character::expgain (int m_exp)
    {

    }

    void Character::levelup (int m_level)
    {

    }

    bool Character::die (bool m_alive)
    {

    }

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
//Character.h


#ifdef DEF_CHARACTER
#define DEF_CHARACTER
#endif // DEF_CHARACTER



class Character{

public:
Character();

    void showlife (int m_vie);
    void getdmg (int m_vie);
    void attack (Character &target);
    void healing (int m_vie);
    void manaregen (int m_mana);
    void expgain (int m_exp);
    void levelup (int m_level);
    bool die (bool m_alive);

private:

int m_vie = 100;
int m_mana = 100;
int m_exp = 0;
int m_level = 1;
bool m_alive = true;
};
The only problem in your code is showlife:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Character{

public:
Character();

    void showlife (int m_vie); // Note: int m_vie

...

    void Character::showlife () // Note: missing int m_vie
    {

    }
even if I wrote that line , I still got an error : undefined reference to 'Character::Character()'
Did you properly add 'Character.cpp' to your project?
The compiler should have complained about about the erroneous showlife(...) function.

By the way:
1
2
3
#ifdef DEF_CHARACTER
#define DEF_CHARACTER
#endif // DEF_CHARACTER 
is not how include guards work:

https://en.wikipedia.org/wiki/Include_guard

Note where the #endif is located.
Yes Character.cpp is in the same project as main.cpp .
As i
I see the #endif should be located at the end of the class definition ?
The whole point of those include guards is to ensure that:

1) if this is the first time the file has been included in the translation unit, then the entire contents of the header file are included.

2) if this is the second or later time the file has been included, then the entire contents of the header file are excluded.

So the entire contents of your header file need to be wrapped by the include guard.

You should actually try and understand how it works, rather than just considering it to be a bit of magic you type because you're told to.
Last edited on
I'll try it later when I'll get back home ,
I was just trying to figure it out in my head for now , thank you guys
I fixed it a little bit, but there's still something with it.
I have replaced #ifdef DEF_CHARACTER by#ifndef DEF_Character Then..
I wrapped the header with the include guard like you said and I came up with something like this
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
#ifndef DEF_CHARACTER
#define DEF_CHARACTER

#include <string>


class Character{

public:
Character();

    void showlife (int m_vie);
    void getdmg (int m_vie);
    void attack (Character &target);
    void healing (int m_vie);
    void manaregen (int m_mana);
    void expgain (int m_exp);
    void levelup (int m_level);
    bool die (bool m_alive);

private:

int m_vie = 100;
int m_mana = 100;
int m_exp = 0;
int m_level = 1;
bool m_alive = true;
};


#endif 


And I still get the same error from the compiler undefined reference to 'Character::Character()'
What IDE do you use? It looks like that Character.cpp is ignored by the compiler otherwise it should have complained about the missing int m_vie. So it cannot be properly added to your project.

The include guard wasn't the source of your problem, just a side note
I use Codeblock, and the problem was into the property of Character.h
I check link file and compile and then it works , I didn't check there before
I got another warning from the compiler about all my attribute in private
The compiler said that there are all non static data member. What is the purpose of that?

By the way int m_vie was added to the showlife method, thank you
Last edited on
Topic archived. No new replies allowed.