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