简体   繁体   中英

C++ Incomplete Type Error

(I have read all the threads posted here and google, I was not able to fix from that)

I am having toruble with a incomplete type error when compiling. The way I am designing the project, the game pointer is unavoidable.

main.cpp
#include "game.h"
// I actually declare game as a global, and hand itself its pointer (had trouble doing it with "this")
Game game;
Game* gamePtr = &game;
game.init(gamePtr);
game.gamePtr->map->test(); // error here, I also tested the basic test in all other parts of code, always incomplete type.


game.h
#include "map.h"
class Map;

class Game {

    private:
        Map *map;
        Game* gamePtr;

    public:
        void init(Game* ownPtr);
        int getTestInt();
};


game.cpp
#include "game.h"

void Game::init(Game* ownPtr) {
    gamePtr = ownPtr;
    map = new Map(gamePtr); // acts as "parent" to refer back (is needed.)
}

int Game::getTestInt() {
    return 5;    
}


map.h
class Game;

class Map {
    private:
        Game* gamePtr;
    public:
        int test();
};

map.cpp 
#include "map.h"

int Map::test() {
    return gamePtr->getTestInt();
}

// returns that class Game is an incomplete type, and cannot figure out how to fix.

Let's go over the errors:

1) In main , this is an error:

    game.gamePtr->map->test(); 

The gamePtr and map are a private members of Game , therefore they cannot be accessed.

2) The Map is missing a constructor that takes a Game* in Game.cpp .

    map = new Map(gamePtr); 

Here is a full working example that compiles. You have to provide the functions that are missing bodies, such as Map(Game*) .

game.h

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

class Map;
class Game {
    private:
        Map *map;
    public:
        Game* gamePtr;
        void init(Game* ownPtr);
        int getTestInt();
    };
#endif

game.cpp

#include "game.h"
#include "map.h"

void Game::init(Game* ownPtr) {
    gamePtr = ownPtr;
    map = new Map(gamePtr); // acts as "parent" to refer back (is needed.)
}

int Game::getTestInt() {
    return 5;    
}

map.h

#ifndef MAP_H_INCLUDED
#define MAP_H_INCLUDED

class Game;
class Map {
    private:
        Game* gamePtr;
    public:
        int test();
        Map(Game*);
};
#endif

map.cpp

#include "game.h"
#include "map.h"

int Map::test() {
    return gamePtr->getTestInt();
}

main.cpp

#include "game.h"
#include "map.h"

int main()
{
    Game game;
    Game* gamePtr = &game;
    game.init(gamePtr);
    game.gamePtr->map->test(); 
}

After doing this and creating a project in Visual Studio, I do not get any errors building the application.

Note the usage of #include guards , which your original posted code did not have. I also placed the members that were private and moved them to public in the Game class, so that main() can compile successfully.

You need to use forward declaration. Place declaration of Map class before definition of class Game:

game.h

class Map; // this is forward declaration of class Map. Now you may have pointers of that type
class Game {

    private:
        Map *map;
        Game* gamePtr;

    public:
        void init(Game* ownPtr);
        int getTestInt();
};

Every place where you use Map and Game class by creating instance of it or dereferencing pointer to it either through -> or * you have to make that type "complete". It means that main.cpp must include map.h and map.cpp must include game.h directly or indirectly.

Note you forward declare class Game to avoid game.h to be included by map.h , and that is fine and proper, but map.cpp must have game.h included as you dereference pointer to class Game there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM