error C2079: ... uses undefined struct ...

closed account (10oTURfi)
Long code short:
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
class Creature
{
    Creature(/*...*/) {}
};

struct Enemy : public Creature
{
    Enemy(/*...*/) : Creature(/*...*/)
}

class Combat
{
    Enemy enemy;
    Combat(/*...*/, Enemy enemy) : enemy(enemy) {}
}

class Game
{
    std::vector<Enemy> Enemies;
    Foo()
    {
        Enemy enemy(/*...*/)
        int rand = urand(0, Enemies.size()-1);
        Combat Combat(/*...*/, Enemies[rand])
    }
}


Gives an error:
error C2079: 'Combat::enemy' uses undefined struct 'Enemy'


Where is the problem?
Which line?
You're missing semicolons after the class declarations.
closed account (10oTURfi)
1
2
Which line?
You're missing semicolons after the class declarations. 


Hmm that was just a mockup. I fixed those semicolons and it seems problem wasnt there. Error was at line 13, Enemy enemy

Edit: I guess i'll post whole thing when i get back to my computer
Last edited on
Are these in separate files?
closed account (10oTURfi)
Yes
Can you show which classes are in which headers and how each implementation #includes them?
closed account (10oTURfi)
Yeah. I've cut some parts a bit but it should do:
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
#ifndef CREATURE_H
#define CREATURE_H

class Creature;

#include "include.h"
#include "spell.h"

class Creature
{
    //blah blah boring stuff here
public:
    Creature() {}
    ~Creature() {}

    Creature(int Atk, int Armor, int Health, int x, int y, int Level, std::string Name)
    {
        //...
    }
    Creature(int Atk, int Armor, int Health, int Level, std::string Name)
    {
        //...
    }
};

#endif 


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
#ifndef ENEMY_H
#define ENEMY_H

struct Enemy;

#include <SFML\Graphics.hpp>
#include "Include.h"
#include "item.h"
#include "player.h"
#include "creature.h"

struct Enemy : public Creature
{
    Enemy(int Atk, int Def, int Health, int x, int y, std::string Alive, std::string Combat, int Level, std::string Name);
    Enemy(int Atk, int Def, int Health, std::string Combat, int Level, std::string Name);
    ~Enemy();

    //...

    struct Loot
    {
        Loot(Item* Item, int Chance) : Item(Item), Chance(Chance) {}
        int Chance;
        Item* Item;
    };
    std::vector<Loot> Loot;
};

#endif 


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

class Combat;

#include "game.h"
#include "enemy.h"
#include "spell.h"
#include "creature.h"

class Combat
{
    sf::RenderWindow &window;
    Player &player;
    Enemy enemy;
    //....
public:
    //...
    Combat(sf::RenderWindow &window, Player &player, Enemy enemy);
    int MainLoop();
};

#endif 


1
2
3
4
5
6
7
//Combat.cpp
#include "combat.h"

Combat::Combat(sf::RenderWindow &window, Player &player, Enemy enemy) : window(window), player(player), enemy(enemy)
{
    TextY = 510;
}


1
2
3
4
//Somewhere in game.cpp Combat object is created
Combat Combat(Window, player, *itr);
Combat.MainLoop();
//itr is std::vector<Enemy>::iterator 
Last edited on
I dont' see it directly, but I'm willing to bet you have a circular inclusion problem. You appear to be just including all headers willy-nilly. Long story short, combat.h is including enemy.h, which is including some other header that includes enemy.h. The circular inclusion means that your Combat class is being defined before your Enemy class is.

Only include headers you need. See sections 4 and 5 of this article:

http://www.cplusplus.com/forum/articles/10627/#msg49679
Topic archived. No new replies allowed.