2012-11-01 107 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

相关问题