C++ C++;不完全类型错误

C++ C++;不完全类型错误,c++,types,C++,Types,(我已经阅读了这里和谷歌发布的所有帖子,我无法从中修复) 我在编译时遇到toruble,但类型错误不完整。在我设计项目的方式中,游戏指针是不可避免的 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.in

(我已经阅读了这里和谷歌发布的所有帖子,我无法从中修复)

我在编译时遇到toruble,但类型错误不完整。在我设计项目的方式中,游戏指针是不可避免的

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.

您需要使用转发声明。将地图类声明放在类游戏定义之前:

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();
};

您需要使用转发声明。将地图类声明放在类游戏定义之前:

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();
};

您需要使用转发声明。将地图类声明放在类游戏定义之前:

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();
};

您需要使用转发声明。将地图类声明放在类游戏定义之前:

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();
};

在使用
地图
游戏
类的每个地方,通过创建实例或通过->或*取消引用指向该类的指针,必须将该类型设置为“完成”。这意味着
main.cpp
必须包括
map.h
map.cpp
必须直接或间接包括
game.h


请注意,您向前声明
类游戏
,以避免
游戏.h
包含在
地图.h
中,这是正确的,但是
map.cpp
必须包含
game.h
,因为您在那里取消引用指向类
game
的指针。

您使用
map
game
类的每个地方都必须创建它的实例,或者通过->或*取消引用指向它的指针,您必须将该类型设置为“完成”。这意味着
main.cpp
必须包括
map.h
map.cpp
必须直接或间接包括
game.h


请注意,您向前声明
类游戏
,以避免
游戏.h
包含在
地图.h
中,这是正确的,但是
map.cpp
必须包含
game.h
,因为您在那里取消引用指向类
game
的指针。

您使用
map
game
类的每个地方都必须创建它的实例,或者通过->或*取消引用指向它的指针,您必须将该类型设置为“完成”。这意味着
main.cpp
必须包括
map.h
map.cpp
必须直接或间接包括
game.h


请注意,您向前声明
类游戏
,以避免
游戏.h
包含在
地图.h
中,这是正确的,但是
map.cpp
必须包含
game.h
,因为您在那里取消引用指向类
game
的指针。

您使用
map
game
类的每个地方都必须创建它的实例,或者通过->或*取消引用指向它的指针,您必须将该类型设置为“完成”。这意味着
main.cpp
必须包括
map.h
map.cpp
必须直接或间接包括
game.h


注意,您向前声明
类游戏
,以避免
游戏.h
映射.h
包含,这是正确的,但是
映射.cpp
必须包含
游戏.h
,因为您在那里取消引用指向类
游戏
的指针。

让我们检查错误:

1) 在
main
中,这是一个错误:

    game.gamePtr->map->test(); 
gamePtr
map
Game
private
成员,因此无法访问它们

2)
Map
缺少在
Game.cpp
中接受
Game*
的构造函数

    map = new Map(gamePtr); 

下面是一个完整的编译示例。您必须提供缺少实体的函数,例如
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;    
}
#include "game.h"
#include "map.h"

int Map::test() {
    return gamePtr->getTestInt();
}
#include "game.h"
#include "map.h"

int main()
{
    Game game;
    Game* gamePtr = &game;
    game.init(gamePtr);
    game.gamePtr->map->test(); 
}
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"

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

int Game::getTestInt() {
    return 5;    
}
#include "game.h"
#include "map.h"

int Map::test() {
    return gamePtr->getTestInt();
}
#include "game.h"
#include "map.h"

int main()
{
    Game game;
    Game* gamePtr = &game;
    game.init(gamePtr);
    game.gamePtr->map->test(); 
}
main.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;    
}
#include "game.h"
#include "map.h"

int Map::test() {
    return gamePtr->getTestInt();
}
#include "game.h"
#include "map.h"

int main()
{
    Game game;
    Game* gamePtr = &game;
    game.init(gamePtr);
    game.gamePtr->map->test(); 
}
完成此操作并在VisualStudio中创建项目后,我在构建应用程序时没有遇到任何错误


注意
#include guards
的用法,这是您最初发布的代码所没有的。我还将
private
的成员放置在
Game
类中,并将其移动到
public
,以便
main()
可以成功编译。

让我们回顾一下错误:

1) 在
main
中,这是一个错误:

    game.gamePtr->map->test(); 
gamePtr
map
Game
private
成员,因此无法访问它们

2)
Map
缺少在
Game.cpp
中接受
Game*
的构造函数

    map = new Map(gamePtr); 

下面是一个完整的编译示例。您必须提供缺少实体的函数,例如
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;    
}
#include "game.h"
#include "map.h"

int Map::test() {
    return gamePtr->getTestInt();
}
#include "game.h"
#include "map.h"

int main()
{
    Game game;
    Game* gamePtr = &game;
    game.init(gamePtr);
    game.gamePtr->map->test(); 
}
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"

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

int Game::getTestInt() {
    return 5;    
}
#include "game.h"
#include "map.h"

int Map::test() {
    return gamePtr->getTestInt();
}
#include "game.h"
#include "map.h"

int main()
{
    Game game;
    Game* gamePtr = &game;
    game.init(gamePtr);
    game.gamePtr->map->test(); 
}
main.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;    
}
#include "game.h"
#include "map.h"

int Map::test() {
    return gamePtr->getTestInt();
}
#include "game.h"
#include "map.h"

int main()
{
    Game game;
    Game* gamePtr = &game;
    game.init(gamePtr);
    game.gamePtr->map->test(); 
}
完成此操作并在VisualStudio中创建项目后,我在构建应用程序时没有遇到任何错误


注意
#include guards
的用法,这是您最初发布的代码所没有的。我还将
private
的成员放置在
Game
类中,并将其移动到
public
,以便
main()
可以成功编译。

让我们回顾一下错误:

1) 在
main
中,这是一个错误:

    game.gamePtr->map->test(); 
gamePtr
map
Game
private
成员,因此无法访问它们

2)
Map
缺少在
Game.cpp
中接受
Game*
的构造函数

    map = new Map(gamePtr); 

下面是一个完整的编译示例。您必须提供缺少实体的函数,例如
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;    
}
#include "game.h"
#include "map.h"

int Map::test() {
    return gamePtr->getTestInt();
}
#include "game.h"
#include "map.h"

int main()
{
    Game game;
    Game* gamePtr = &game;
    game.init(gamePtr);
    game.gamePtr->map->test(); 
}
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"

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

int Game::getTestInt() {
    return 5;    
}
#include "game.h"
#include "map.h"

int Map::test() {
    return gamePtr->getTestInt();
}
#include "game.h"
#include "map.h"

int main()
{
    Game game;
    Game* gamePtr = &game;
    game.init(gamePtr);
    game.gamePtr->map->test(); 
}
main.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;    
}
#include "game.h"
#include "map.h"

int Map::test() {
    return gamePtr->getTestInt();
}
#include "game.h"
#include "map.h"

int main()
{
    Game game;
    Game* gamePtr = &game;
    game.init(gamePtr);
    game.gamePtr->map->test(); 
}
在完成此操作并在VisualStudio中创建项目之后,我在构建应用程序时没有遇到任何错误