Declaration is incompatible error

Jan 20, 2012 at 1:21pm
Whenever I compile this, the skeleton class and the player class cannot pass the respective other object in. Please help.

The error happens on 34/51 (prototype/declaration) and 47/67 (prototype/declaration).
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Dungeons Game
// A Win32 Console based pseudo-RPG
#include <iostream>
using namespace std;

// Begin LivingThing class
class LivingThing
{
public:
    // Constructors
    LivingThing() {}
    ~LivingThing() {}

    // Accessors
    void DisplayHealth() const { cout << m_Attack; }
    bool IsAlive() const { return m_Alive; }

protected:
    unsigned short m_Attack;
    unsigned short m_Mana;
    short m_Health;
    bool m_Alive;
};

// Begin Player class
class Player : public LivingThing
{
public:
    // Constructors
    Player() { m_Attack = 7; m_Health = 30; m_Mana; }
    ~Player() {}

    // Dealing damage and taking damage functions
    void Attack(Skeleton& skeletonInstance);
    void TakeDamage(int damage);
};

// Begin Skeleton class
class Skeleton : public LivingThing
{
public:
    // Constructors
    Skeleton() { m_Attack = 5; m_Health = 10; m_Mana = 0; }
    ~Skeleton() {}

    // Dealing damage and taking damage functions
    void Attack(Player& playerInstance);
    void TakeDamage(int damage);
};

void Player::Attack(Skeleton& skeletonInstance)
{
    cout << "Player attacks 'Skeleton' for " << m_Attack << " damage.";
    skeletonInstance.TakeDamage(m_Attack);
}

void Player::TakeDamage(int damage)
{
    m_Health -= damage;
    if (m_Health <= 0)
    {
        cout << "Player is dead";
        m_Alive = false;
    }
}

void Skeleton::Attack(Player& playerInstance)
{
    cout << "Skeleton attacks 'Player' for " << m_Attack << " damage.";
    playerInstance.TakeDamage(m_Attack);
}

void Skeleton::TakeDamage(int damage)
{
    m_Health -= damage;
    if (m_Health <= 0)
    {
        cout << "Skeleton is dead";
        m_Alive = false;
    }
}


// Main entry point
int main()
{
    enum OBJECTS {player, skeleton};

    short skeletonKillCount = 0;
    // Create a player instance
    Player player1;

    // Create three Skeleton instances
    Skeleton skeleton1;
    Skeleton skeleton2;
    Skeleton skeleton3;
    Skeleton curSkeleton;

    // Run until either player is dead or all 3 skeletons have been defeated
    // Main game loop
    do 
    {
        // Assign the skeleton number to the curSkeleton variable
        if (skeleton1.IsAlive()) {
            Skeleton curSkeleton = skeleton1;
        }
        else if (skeleton2.IsAlive())
        {
            Skeleton curSkeleton = skeleton2;
        }
        else if (skeleton3.IsAlive())
        {
            Skeleton curSkeleton = skeleton3;
        }

        // Perform the actions
        player1.Attack(curSkeleton);
        curSkeleton.Attack(player1);
        system("pause");

        // Display all healths
        player1.DisplayHealth();
        curSkeleton.DisplayHealth();
        
        if (curSkeleton.IsAlive() == false) {
            skeletonKillCount++;
        }

        if (curSkeleton.IsAlive() == false) {
            cout << "You died after killing " << skeletonKillCount << " skeletons. Better luck next time.";
            system("pause");
            return 0;
        }

    } while (skeletonKillCount < 3);

    cout << "You survived! You killed all 3 skeletons with ";
    player1.DisplayHealth();
    cout << " health left.";

    system("pause");
    return 0;
}
Jan 20, 2012 at 1:25pm
The problem is that you try use Skeleton before it has been declared. A simple forward declaration above 26 will make it work.
class Skeleton;
Jan 20, 2012 at 1:46pm
Thanks. That did the trick.
Topic archived. No new replies allowed.