[英]Unknown type name in C++
I keep getting "Unknown type name ' Life
'" in world.h
and "Unknown type name ' Life
'" and "Unknown type name ' World
'" in game.h
.我不断收到“未知类型名称‘ Life
’”,在world.h
和“未知类型名称‘ Life
’”和“未知类型名称‘ World
’”在game.h
。
I have a feeling that there might be too many include statements based on other posts but I can't find the solution in this case.我有一种感觉,可能有太多基于其他帖子的包含语句,但在这种情况下我找不到解决方案。
Any help/hints will be greatly appreciated任何帮助/提示将不胜感激
game.h游戏.h
#pragma once
#ifndef LIFE_H
#include "life.h"
#define LIFE_H
#endif
#ifndef WORLD_H
#include "world.h"
#define WORLD_H
#endif
class Game {
public:
// Constructor/destructor
Game(Life **life, int numLife);
~Game();
void game_loop();
private:
World * m_world;
int m_steps;
bool m_automate;
};
world.h世界.h
#pragma once
#ifndef LIFE_H
#include "life.h"
#define LIFE_H
#endif
#ifndef WORLD_H
#include "world.h"
#define WORLD_H
#endif
class World {
public:
// Constructor/destructor
World();
~World();
void print() const;
bool initLife(Life *life);
void updateWorld();
private:
char getNextState(char state, char row, char col, bool toggle) const;
char **m_grid_1;
char **m_grid_2;
bool m_toggle;
};
life.h (without any errors) life.h(没有任何错误)
#ifndef life_h
#define life_h
#ifndef life_h
#include "life.h"
#define life_h
#endif
#ifndef GAME_H
#include "game.h"
#define GAME_H
#endif
class Life {
public:
int getCol() const; // const member functions cannot modify member variables.
int getRow() const;
int getHeight() const;
int getWidth() const;
char getFromFigure(int r, int c) const;
protected:
int m_col;
int m_row;
int m_height;
int m_width;
char **m_sprite;
World *m_world;
};
#endif
You're effectively taking careful aim and shooting yourself precisely in the foot here:你在这里有效地仔细瞄准并精确地用脚射击自己:
#ifndef life_h
#define life_h
#ifndef life_h
#include "life.h"
#define life_h
#endif
Notice you define the very thing that prevents including your own header file.请注意,您定义了阻止包含您自己的头文件的内容。
This is pretty similar to code like:这与以下代码非常相似:
int loaded = 1;
if (!loaded) {
// Why doesn't this run!?
}
If your target compiler(s) support #pragma once
then remove all of this and just have #pragma once
in each of your header files near the top.如果您的目标编译器支持#pragma once
则删除所有这些,并在靠近顶部的每个头文件中使用#pragma once
。
Otherwise you need to gate the files individually, only :否则,您需要单独对文件进行门控,只需:
// example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
...
#endif
Where the name of the #define
is derived from the filename and is unique within the entire application. #define
的名称源自文件名并且在整个应用程序中是唯一的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.