不完整的类型不允许错误

Incomplete type not allowed error

本文关键字:不允许 错误 类型      更新时间:2023-10-16

我创建了一个使用OpenGL显示标签(GameLabel)的类,我遇到了我无法解决的2个非常奇怪的错误。

错误c2079:'displayplayer'使用未定义的类'gamelabel'Intellisense:不允许使用不完整的类型

这是我的代码;

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;
}

可能是由于循环依赖吗?gamelabel包括game.h ..

在您的所有.h文件上都写在最顶部:

#ifndef YOUR_FILE_NAME_H_
#define YOUR_FILE_NAME_H_

这是最底层的:

#endif

替换您使用的.h文件名替换your_file_name_h_,最好使用大写。

这将防止头部文件多次包含。