I have two classes. One main Game class and an Entity class that needs to point to the Game class so it can call its various functions

Entity.h

class Game;

class Entity
{
public:
    Entity(Game *game);
    ~Entity(void);

    void Draw(float dt);
    void Update(float dt);

protected:
    Game *m_game;           // Missing type specifier error C4430
};

Entity.cpp

#include "Entity.h"

Entity::Entity(Game *game) 
    : m_game(game)                  // Initialise members
{

}

Entity::~Entity(void)
{

}

void Entity::Draw(float dt)
{

}

void Entity::Update(float dt)
{
    m_game->DoSomething();                      // Missing type specifier error C4430
}

I've forward declared my Game class but I can't use it anywhere in the Entity class as I always get

Missing type specifier error C4430

when trying to access it through intellisense (CTRL + J).

My Game header file doesn't appear to cause any circular dependencies.

Game.h

#include <list>

using namespace std;

class Entity;   

class Game
{
public:
    list<Entity*> m_entities;

    Game(void);
    ~Game(void);

    void init();
    void draw(float dt);
    void loadContent();
    void update(float dt);
}

What's going on?

I think you need to declare the Game class in Entity.h like so:

class Game {};

Declaring the Game class with an empty member list should make it a complete declaration. Without the empty member list, the class would have an incomplete declaration, which would not allow any objects of it to be declared.

Edited 4 Years Ago by _avishek

OK, so you've forward declared the classes in the header files. And I could be wrong, but from what I've seen; you haven't included the header files for the forward declared classes in the corresponding .cpp files. Which is most likely why you're getting the error messages you've reported.

So for example, where you've forward declared the Game class in Entity.h, you should be including game.h in Entity.cpp.
Likewise, where you are forward declaring the Entity class in Game.h you will need to include Entity.h in Game.cpp.

Also you might want to consider using some inclusion guards in your headers to prevent things from being included multiple times. Otherwise you'll start getting errors about multiple definitions of the Entity and Game classes.

Edited 4 Years Ago by JasonHippy

Intellisense doesn't like that either.

It will allow me to use

m_game->DoSomething();

but Ctrl + J and scrolling down to m_game will still bring up Missing type specific error C4430 which makes me believe that either Intellisense is wrong or there is a problem with the implementation.

In my experience the intellisense in VS has always been a bit flaky (assuming you're using VS) and shouldn't be relied upon too heavily! I've been using VS at work since version 6. I'm currently using 2008 and I don't think intellisense has ever worked properly. Very often, the intellisense database that VS uses gets corrupted and doesn't always update properly after making changes to problematic code. But you can usually clear up any problems with intellisense (albeit temporarily) by closing your solution, deleting it's corresponding intellisense database and then reopening the solution. Then the intellisense will rebuild the database and it'll be OK until it corrupts again. Which could be seconds, minutes, hours, days or even weeks later. But is guaranteed to happen at some point!

More importantly, what output does the compiler give when you attempt to compile your program? If you get the same errors when you compile your program, then you know for sure that there's a problem. I wouldn't worry too much about errors thrown up by intellisense. As I said, it's always been a bit flaky and problematic and shouldn't be relied upon too heavily! Granted, in this case, it could be correctly reporting a problem, or it could just be that the intellisense database has corrupted and isn't updating properly!

If I had a penny for every time the intellisense database went screwy over the years, I'd probably be as rich as Bill Gates... Well... OK, probably not! But I'd almost certainly have a good £10 or so! Heh heh!

Comments
Great answer and has helped a C# junkie into the world of C++
This question has already been answered. Start a new discussion instead.