【问题标题】:Incomplete type not allowed error不完整类型不允许错误
【发布时间】:2012-11-01 01:28:50
【问题描述】:

我使用 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;

}

【问题讨论】:

  • 愚蠢的问题..但您确实包含了 .h 对吗?这些是编译错误还是只是智能感知错误?
  • 我不是这是你的核心问题,但GameLabel displayPlayer = new GameLabel(...); 对我来说似乎不合适。您是否忘记将* 声明为指针(例如GameLabel * displayPlayer = new ...)?
  • 另一个问题是,在您的 GameLabel 构造函数中,第三个参数是一个字符,但正在尝试使用字符串。
  • Incomplete type not allowed是intelliSense错误,另一个是complie错误。你说得对,我是为了得到一个指针,我现在已经添加了它,但是这似乎没有影响问题。

标签: c++ visual-studio-2010 visual-c++ opengl


【解决方案1】:

会不会是循环依赖? GameLabel 包含 game.h.. 并且游戏似乎也依赖于 gamelabel,Game.h 是否包含 gamelabel.h?

【讨论】:

  • 嗨,不,似乎没有任何循环依赖...我还将 Game.h 包含到 GameLabel 类中,所以它似乎也不是。
  • 您是否已将 GameLabel.h 包含到 Game.cpp 或 Game.h 中?除非必要,最好添加到 Game.cpp,包括到 game.h 会导致循环依赖
  • 将 game.h 包含到 GameLabel.cpp 中似乎可以消除智能错误,谢谢!虽然它似乎仍然无法识别构造函数
  • 您解决了 cmets 中指出的 2 个问题吗? *displayPlayerchar *stristd::string stri?
  • 另外,如果朋友类声明是在gamelabel.h中包含Game.h的唯一目的,我建议你从那里删除它,没有它的朋友声明也可以。
【解决方案2】:

在所有 .h 文件的最顶部写下:

#ifndef YOUR_FILE_NAME_H_
#define YOUR_FILE_NAME_H_

这在最底部:

#endif

将 YOUR_FILE_NAME_H_ 替换为您所在的 .h 文件名,最好使用大写。

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

【讨论】:

  • 或者只是在顶部#pragma once
  • 好的,完成了。虽然它并没有摆脱错误,但谢谢:)
  • @KarthikT 嘿,请注意,#pragma once 并不是真正的标准,尽管大多数编译器都支持它
  • 确实是我自己发现的,但我猜最多会使代码更干净,如果他转向不支持的编译器(罕见),它不应该默默地失败。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-03
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多