【问题标题】:Multiple errors when using gl使用 gl 时出现多个错误
【发布时间】:2016-03-24 07:28:26
【问题描述】:

我正在尝试使用 Opengl、SDL 和 GL 使用 C++ 创建一个 Snake 游戏,尽管我已经正确链接了所有内容我在构建时遇到了一堆错误,我环顾四周发现这些错误在使用 GL 时很常见尽管我似乎无法确定为什么要得到它们,因为我已经采取了预防措施,例如在包含“GL/freeglut.h”之前包含“windows.h”,有人可以帮忙吗?

包含的错误有:

error C2144: syntax error : 'void' should be preceded by ';'
Error   C2144   syntax error: 'void' should be preceded by ';'  OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157
Error   C2182   'APIENTRY': illegal use of type 'void'  OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157
Error   C2146   syntax error: missing ';' before identifier 'glAccum'   OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157
Error   C2144   syntax error: 'void' should be preceded by ';'  OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157
Error   C2182   'APIENTRY': illegal use of type 'void'  OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157
Error   C2146   syntax error: missing ';' before identifier 'glAccum'   OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157

主要 cpp

#ifdef __MINGW32__
#include <windows.h>
#endif

#ifdef WIN32
#include <windows.h>
#endif

#include <GL/freeglut.h>
#include <SDL2/SDL.h>
#include <time.h>
#include "SnakeGame.hpp"


int main(void) {

    srand(time(NULL));

    SnakeGame *f = new SnakeGame();
    f->run();
    return 0;
}

Arena.hpp

#ifdef _WIN32
#include <windows.h>
#endif

#include <GL/freeglut.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

#include "Obstacle.hpp"
#include "Fruit.hpp"
#include "Snake.hpp"
#include "Font.hpp"

class Arena {

 public:

  Arena(void);
  virtual ~Arena(void);
  void update(void);
  inline void draw(void);
  Snake *snake;

 private:
  float rot;
  unsigned int score;
  Font *font;
  inline void correct_elements(void);
  void drawRainbowTriangle(void);
  void correct_fruit(void);
  void correct_obstacle(void);
  void correct_snake(void);
  bool is_snake_dead(void);
  bool is_snake_eating(void);
  Obstacle *obstacle;
  Fruit *fruit;
};

Element.hpp

#include <iostream>
#include <ostream>
#include <GL/freeglut.h>
#include <GL/gl.h>

class Element {

public:
  Element();
  virtual void draw() = 0;
  int getX();
  int getY();
  void setX(int it);
  void setY(int it);
  void display();

protected:
  int x;
  int y;
};

字体.hpp

#include <GL/freeglut.h>
#include <string.h>

class Font {

public:
  void bitmap_output(int x, int y, const char *string, void *font = GLUT_BITMAP_TIMES_ROMAN_24);


private:

};

Fruit.hpp

#include <GL/freeglut.h>
#include <GL/gl.h>

#include "Element.hpp"

class Fruit : public Element {

public:
  void draw();
};

Game.hpp

include <SDL2/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>

class Game {

 public:
  Game();
  ~Game();
  virtual void run()=0;
  int get_score();
  void set_score(int s);
  bool paused;
 private:

 protected:
  SDL_Renderer* displayRenderer;
  SDL_Window* displayWindow;
  SDL_RendererInfo displayRendererInfo;
};

Snake.hpp

#include <iostream>
#include <list>
#include <GL/gl.h>
#include <GL/freeglut.h>
#include "Element.hpp"

enum Dir {UP, DOWN, RIGHT, LEFT};

class Body : public Element {

public:
  void draw() {
    glColor3ub(0, 0, 255);
    glPushMatrix();
    glTranslatef(x, y, -y);
    glutSolidCube(1);
    glPopMatrix();
  }
};

class Snake {

public:
  Snake();
  ~Snake();
  void update(void);
  void draw(void);
  void grow(int size);
  void setDir(Dir direction);
  std::list<Body> body;
  std::list<Body>::iterator iter;

private:
  Dir dir;
};

Snake game.hpp

#ifdef _WIN32
#include <windows.h>
#endif

#include <stdlib.h>
#include <SDL2/SDL.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include "Arena.hpp"
#include "Game.hpp"

class SnakeGame : public Game {

 public:
  SnakeGame();
  virtual ~SnakeGame();
  void run();

 private:
  bool is; //is the game running

  Arena arena;
  void keyboard(const SDL_Event &event);
};

【问题讨论】:

  • 请编辑您的问题以包含未编辑的 complete 错误输出。还要指出 where 在显示的代码中出现错误(例如通过添加注释)。最后,您确实显示的错误说明了glAccum,但您不会在显示的代码中的任何地方调用它。请尝试创建Minimal, Complete, and Verifiable Example 并展示给我们。

标签: c++ opengl sdl glew freeglut


【解决方案1】:

您正在测试错误的预处理器宏。让它读起来像这样

#ifdef _WIN32
#include <windows.h>
#endif

注意_WIN32 中的下划线。无需测试_MINGW32_,因为那里也定义了另一个宏。

【讨论】:

  • 它仍然没有改变任何东西
  • @imbrett:编译器遇到的问题是 APIENTRY 和 WINGDI 宏(在 gl.h 中使用)丢失了。它们是在 windows.h 中定义的,因此在您的项目中的某个地方,您在 windows.h 之前包含 gl.h(直接或间接),这会导致问题。
  • 如果我给你看源代码,你能假设在哪里吗?
  • @imbrett:我会从不同的角度来解决这个问题:在头文件中只添加包含到您绝对需要的内容。例如。除了 Snake.hpp 之外,没有任何标题需要 GL 包含。除了Game.hpp 之外,没有类似的标头需要SDL 标头。了解标头保护(标头周围的 #ifndef x #define x #endif 块以避免多次包含冲突)。最后但同样重要的是,不要将函数实现为类声明的一部分(在标头中),而是在它们自己的编译单元(cpp 文件)中实现它们。遵循这些简单的规则通常可以避免这样的麻烦
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-28
  • 2018-01-28
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
相关资源
最近更新 更多