在我的代码中,我创建了两个类,包括单独的头文件和类文件。
所以在我的" player.h"标题我有:
#ifndef PLAYER_H
#define PLAYER_H
#include "timer.h"
#include "funcs.h"
enum state{IDLE, RUN, JUMPRISE, JUMPFALL};
class Player
{
public:
Player(SDL_Renderer* renderer, SDL_Texture* charSheet, int x, int y, int movespeed, SDL_Rect dimensions);
void show(Timer animTimer);
void handle(const Uint8* keydown, SDL_Rect* rex, int numRex);
int getX();
int getY();
~Player();
private:
SDL_Renderer* m_renderer;
SDL_Texture* m_charSheet;
SDL_Rect m_coords;
int m_movespeed;
bool m_direction;
state animState;
SDL_Rect anim_left_run_clip[8];
SDL_Rect anim_left_idle_clip[8];
SDL_Rect anim_left_jumprise_clip[8];
SDL_Rect anim_left_jumpfall_clip[8];
SDL_Rect anim_right_run_clip[8];
SDL_Rect anim_right_idle_clip[8];
SDL_Rect anim_right_jumprise_clip[8];
SDL_Rect anim_right_jumpfall_clip[8];
};
#endif // PLAYER_H
每次编译时,都会收到错误消息"错误:定时器尚未声明"在包含以下内容的行
void show(Timer animTimer)
我的" timer.h"文件没什么特别的,我也不相信问题就在那里。我已经检查过的事情:
要么我在某处忽略了一些明显的错误,要么我犯了一些我不知道的编码罪。
答案 0 :(得分:1)
最有可能的是,在timer.h中你是#including player.h,然后#includes timer.h。但是由于标题保护,timer.h的内容会被忽略。结果是player.h在timer.h之前被解析。最终结果是你不能以这种方式相互依赖彼此的两个条款。您需要确定哪一个是另一个使用的基本一个。
如果在标题中,A使用B,但B只使用指向A的指针,则可以先放B,然后使用类似的语句转发声明A.
class A;
在那种情况下,B.h没有#include A.h为全班。