2011-05-05 119 views
0

我包括在所謂的「stats.h」C++,#包括問題!

頭我有2類

一個需要的頭一個

的頭,當我嘗試編譯,我得到約50象這樣的錯誤一個

identifier 'Player' 
int assumed 
; before * missing 

任何想知道如何解決這個問題? :(

(這裏相關的代碼)

CLASS A - PLAYER

#include "stats.h" 

class Player 
{ 
public: 
Player(int x, int y); 

Texture *texture; 

int x; 
int y; 

float rotation; 

void Update(double elapsedTime); 
void Draw(Sprite *sprite); 

private: 
Weapon *weapon; 
}; 

CLASS B - 武器

#include "stats.h" 

class Weapon // **CLASS B** 
{ 
public: 
double interval; 

Weapon(Player *player); 

void Update(double elapsedTime); 
void Draw(Sprite *sprite); 

protected: 

private: 
Player *player; 
}; 

STATS.H

#include "Player.hpp" 
#include "Weapon.hpp" 
+0

你爲什麼要把所有包含在一個頭文件中? – Bart 2011-05-05 07:54:20

+0

@Bart - 有時非常有用 - 例如,當你有一些共同的標題,並且你不想將它們一個接一個地包含在其他幾個文件中時,那麼你可以將include-s放到一個標題中,幷包含它,而不是包括幾個標題。 – 2011-05-05 08:13:29

+0

「,你不想一個一個包含它們......」我仍然儘可能避免這種情況。你可以爲自己創造一個受傷的世界。甚至有人必須接管你的代碼。特別是如果它超出了幾個文件。 – Bart 2011-05-05 08:16:41

回答

1

您的頭文件中沒有實現,因此您可以傳遞前向聲明並僅在cpp文件中包含頭文件。


例如 - class Player之前,提出的class Weapon聲明:

//vvvvvvvvvvv 
class Weapon; 

class Player 
{ 
public: 
    Player(int x, int y); 
    /* .. */ 
}; 

並取出#include "stats.h"class Weapon

+0

類定義在標題中,所以我需要在其中包含player.h – Blaxx 2011-05-05 07:58:05

+0

你的意思是,這個實現是在頭文件中嗎?然後分開它(: – 2011-05-05 07:59:55

+0

確定這工作,謝謝你,可以接受在20秒 – Blaxx 2011-05-05 08:07:42

0

雖然使用正如他人所建議的正向聲明將工作,但您的真正問題在於設計。像這樣的類之間的相互依賴幾乎總是表明設計有問題。在這種情況下,我會真的想知道武器是否需要了解Player。