【问题标题】:Glut in Dev C++ error "redeclaration of C++ built-in type `short'"Dev C++ 错误“重新声明 C++ 内置类型‘short’”中的过剩
【发布时间】:2013-04-04 04:55:20
【问题描述】:

我有这个简单的三角形绘制代码,它会产生错误“重新声明 C++ 内置类型 short”。但是当我将#include<iostream.h> 放在#include<glut.h> 之前时,它会编译并运行。 谁能解释一下这背后的逻辑?

#include<glut.h>
void renderScene(void) {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
        glVertex3f(-0.5,-0.5,0.0);
        glVertex3f(0.5,0.0,0.0);
        glVertex3f(0.0,0.5,0.0);
    glEnd();

        glutSwapBuffers();
}

int main(int argc, char **argv) {

    // init GLUT and create Window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("My first program");

    // register callbacks
    glutDisplayFunc(renderScene);

    // enter GLUT event processing cycle
    glutMainLoop();
    
    return 1;
}

【问题讨论】:

  • 主要问题是“为什么我把 iostream.h 放在首位时它会起作用”?
  • @wallyk 这会是更好的标题!但我需要它起作用的原因?
  • 我建议你看看这两个版本的预处理器输出,也许你能从中找出原因。
  • 请发布完整的编译器调用和输出。
  • 我不确定你的问题,但 iostream 不是 .h 文件,你应该使用 '#include ' 结尾不带 '.h'。

标签: c++ opengl compiler-construction glut dev-c++


【解决方案1】:

我也遇到过同样的问题,把“glut.h”文件中的wchar_t变量名改成wchar_tt,效果很好。

#  ifndef _WCHAR_T_DEFINED
typedef unsigned short wchar_tt;
#   define _WCHAR_T_DEFINED
#  endif
# endif

【讨论】:

    【解决方案2】:

    在我的赛车项目中,我遇到了error redeclaration of c++ built-in type 'wchar_t'。我在谷歌搜索,但没有找到任何解决我的问题的解决方案:-(

    但后来,我自己通过包含“windows.h”解决了这个问题:-)

    #include<windows.h>
    #include<bits/stdc++.h>
    #include<GL/glut.h>
    

    #include&lt;windows.h&gt; 必须添加到顶部。如果在glut.h下添加会报错。

    #include&lt;bits/stdc++.h&gt; 只是为了安全而添加的:-p

    使用#include&lt;GL/glut.h&gt; 或#include&lt;glut.h&gt;,具体取决于放置glut.h 的文件夹。

    【讨论】:

    【解决方案3】:

    很难说没有看到您的确切 glut.h 和库版本,但我看到了 glut.h 的回旋处第 45 行:

       /* XXX This is from Win32's <ctype.h> */
    #  ifndef _WCHAR_T_DEFINED
    typedef unsigned short wchar_t;
    #   define _WCHAR_T_DEFINED
    #  endif
    

    如果wchar_t 已定义(例如short),但_WCHAR_T_DEFINED 宏未定义,则该行将被视为:

    typedef unsigned short short;
    

    这是对内置类型的重新声明。 &lt;iostream&gt;(不要使用 .h 顺便说一句,根据标准不再使用它)正在添加定义以使 typedef 不被执行,或者如果它是一个宏以使 typedef 是合法的,则取消定义 wchar_t。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    相关资源
    最近更新 更多