Plethora of build errrors

So, I was in the process of making a test for some target acquiring, and I got hit with a fair amount of build errors.

Header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef ENEMYLIST_H
#define ENEMYLIST_H

class EnemyList
{
    private:
        Enemy* enemyList;

    public:
        EnemyList();
        ~EnemyList();
        void addEnemy(Enemy enemy);
        void removeEnemy(Enemy enemy);
};
#endif 


Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "EnemyList.h"
#include <vector>

EnemyList::EnemyList()
{
    enemyList = new std::vector<Enemy>;
}

EnemyList::~EnemyList()
{
    delete[] enemyList;
}

void EnemyList::addEnemy(Enemy enemy)
{
    enemyList.push_back(enemy);
}


Aaand build log
C:\Users\Jared\Documents\Programs\EnemyList.h:13: error: 'Enemy' has not been declared
C:\Users\Jared\Documents\Programs\EnemyList.cpp: In constructor 'EnemyList::EnemyList()':
C:\Users\Jared\Documents\Programs\EnemyList.cpp:6: error: 'enemyList' was not declared in this scope
C:\Users\Jared\Documents\Programs\EnemyList.cpp:6: error: 'Enemy' was not declared in this scope
C:\Users\Jared\Documents\Programs\EnemyList.cpp:6: error: template argument 1 is invalid
C:\Users\Jared\Documents\Programs\EnemyList.cpp:6: error: template argument 2 is invalid
C:\Users\Jared\Documents\Programs\EnemyList.cpp: In destructor 'EnemyList::~EnemyList()':
C:\Users\Jared\Documents\Programs\EnemyList.cpp:11: error: 'enemyList' was not declared in this scope
C:\Users\Jared\Documents\Programs\EnemyList.cpp: At global scope:
C:\Users\Jared\Documents\Programs\EnemyList.cpp:14: error: variable or field 'addEnemy' declared void
C:\Users\Jared\Documents\Programs\EnemyList.cpp:14: error: 'Enemy' was not declared in this scope
C:\Users\Jared\Documents\Programs\EnemyList.cpp:18: error: variable or field 'removeEnemy' declared void
C:\Users\Jared\Documents\Programs\EnemyList.cpp:18: error: 'Enemy' was not declared in this scope


Not sure what the issue is, but I'm sure I'm gonna feel dumb when it's pointed out to me
Did you forget to forward declare the class Enemy in your header?
I've never used forward declaration. I kind of know what it is, not sure how to use it though.

Including Enemy.h did fix most of these problems. How would I use forward declaration here?

Is there a reason I can't do this:

1
2
Enemy* enemyList
enemyList = new vector<Enemy>;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef ENEMYLIST_H
#define ENEMYLIST_H

class Enemy;//pre declaration
class EnemyList
{
    private:
        Enemy* enemyList;

    public:
        EnemyList();
        ~EnemyList();
        void addEnemy(Enemy enemy);
        void removeEnemy(Enemy enemy);
};
#endif  


ResidentBiscuit wrote:
Is there a reason I can't do this:
1
2
Enemy* enemyList
enemyList = new vector<Enemy>;


Unless enemyList is a pointer to a vector, it doesn't make sense. you can do Enemy* enemies = new Enemy[x]; or std::vector<Enemy> enemies; or std::vector<Enemy*> enemies;
Last edited on
Ah I thought it would work similar to arrays. I changed it up and it works fine now.

The forward declaration brings up all sorts of errors though. Just including the enemy.h seems to work fine. Is there a reason to use one or the other?
Use forward declaration when you are only using pointers. Include the header in other cases. Without forward declaration, this would be impossible:
1
2
3
4
5
6
class A {
    B* b; // error, B not yet defined
};
class B {
    A* a;
};
Oooh, that makes sense. Hmm, well I don't think I need pointers here. I can just pass out a pointer to the vector when I need it.
What is the point of the EnemyList class? Is there any reason you can't just use an std::list<Enemy> where it is needed?
I was trying to be fancy. I now realize that it just complicated things. As I can't reasonably pass it anywhere.

Think it's time to scratch the class
Topic archived. No new replies allowed.