2012-11-01 105 views
1

我創建了一個類,它使用openGL顯示標籤(GameLabel),我得到了2個非常奇怪的錯誤,我無法解決。不完整類型不允許錯誤

錯誤C2079: 'displayPlayer' 使用未定義類GameLabel' 智能感知:不完全類型是不允許

這裏是我的代碼;

在Game.cpp的函數,它們調用標籤類

void Game::draw(SDL_Window *window) 
{ 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear window 

// draw player 
glColor3f(1.0,1.0,1.0); 
glBegin(GL_POLYGON); 
    glVertex3f (xpos, ypos, 0.0); // first corner 
    glVertex3f (xpos+xsize, ypos, 0.0); // second corner 
    glVertex3f (xpos+xsize, ypos+ysize, 0.0); // third corner 
    glVertex3f (xpos, ypos+ysize, 0.0); // fourth corner 
glEnd(); 
GameLabel displayPlayer = new GameLabel(xpos+(xsize/2.0f), ypos+ysize, "Player"); 
//The ablove line is the one flagging the errors. 

現在這裏是GameLabel.h。

#include <SDL_ttf.h> 
#include <GL/glew.h> 
#include <string> 
#include "Game.h" 
class GameLabel 
{ 
public: 
    GameLabel(float fx, float fy, char stri); 
~GameLabel(void); 
void textToTexture(const char * str, SDL_Surface* stringImage); 
void draw(SDL_Surface* stringImage); 
friend class Game; 

protected: 
SDL_Surface stringImage; 
private: 
GLuint texID; 
GLuint height; 
GLuint width; 
GLfloat x; 
GLfloat y; 
char str; 
}; 

最後GameLabel.cpp構造

GameLabel::GameLabel(float fx, float fy, char stri) 
{ 
x = fx; 
y = fy; 
str = stri; 

} 
+3

愚蠢的問題..但你確實包括.h對嗎?這些編譯錯誤還是隻是intellisense錯誤? –

+1

我不是這是你的核心問題,而是'GameLabel displayPlayer = new GameLabel(...);'似乎對我而言。你忘了'*'來聲明'displayPlayer'作爲一個指針(例如'GameLabel * displayPlayer = new ...')? –

+1

另一個問題是,在你的GameLabel構造函數中,第三個參數是一個char,但是試圖使用一個字符串。 – imreal

回答

2

這可能是由於圓形扶養? GameLabel包含game.h ..並且遊戲似乎也取決於gamelabel,Game.h是否包含gamelabel.h?

+0

嗨,沒有看起來沒有任何循環依賴......我還包括Game.h到GameLabel類,所以它似乎也不是那個。 – SweetDec

+0

您是否將GameLabel.h包含到Game.cpp或Game.h中?除非必要,否則更喜歡添加到Game.cpp中,包括game.h會導致循環依賴 –

+0

將Game.h包括到GameLabel.cpp中似乎會降低intellisence錯誤,謝謝!雖然 – SweetDec

1

在所有的.h文件寫在最高層:

#ifndef YOUR_FILE_NAME_H_ 
#define YOUR_FILE_NAME_H_ 

而且是在最底部:

#endif 

更換YOUR_FILE_NAME_H_的.H文件名你是,最好大寫。

這將防止多次包含頭文件。

+1

或者只在頂部有#pragma一次 –

+0

好吧,做完了。它雖然沒有擺脫錯誤,但謝謝:) – SweetDec

+1

@KarthikT嘿只是一個說明,#pragma曾經不是真正的標準,雖然大多數編譯器支持它 – dchhetri

相關問題