C++ 程序和作用域变量

c++ program and scope variables

我用 dev-c++ 编译了这个 c++ 程序,并为所有变量提供了 "was not declared in this scope"。

#include <cstdlib>                      // standard definitions
#include <iostream>                     // C++ I/O
#include <cstdio>                       // C I/O (for sprintf) 
#include <cmath>                        // standard definitions

#include <GL/glut.h>                    // GLUT
#include <GL/glu.h>                     // GLU
#include <GL/gl.h>                      // OpenGL

using namespace std;                    // make std accessible

//-----------------------------------------------------------------------
// Global data
//-----------------------------------------------------------------------

GLint TIMER_DELAY = 10000;                      // timer delay (10 seconds)
GLfloat RED_RGB[] = {1.0, 0.0, 0.0};            // drawing colors
GLfloat BLUE_RGB[] = {0.0, 0.0, 1.0};

//-----------------------------------------------------------------------
//  Global variables
//-----------------------------------------------------------------------
static bool isReversed = false;                 // draw reversed colors?

//-----------------------------------------------------------------------
//  Callbacks
//      The global variable "isReversed" describes the drawing state.
//      When false, a blue rectangle is drawn on top of red diamond.
//      When true the colors are reversed.  The "isReversed" variable is
//      complemented whenever the left mouse button is clicked or the
//      timer goes off (every 10 seconds).
//-----------------------------------------------------------------------

void myReshape(int w, int h) {
    cout << "MyReshape called width=" << w << " height=" << h << endl;
    glViewport (0, 0, w, h);                    // update the viewport
    glMatrixMode(GL_PROJECTION);                // update projection
    glLoadIdentity();
    gluOrtho2D(0.0, 1.0, 0.0, 1.0);             // map unit square to viewport
    glMatrixMode(GL_MODELVIEW);
    glutPostRedisplay();                        // request redisplay
}
                                                // draw diamond and rectangle
void drawObjects(GLfloat* diamColor, GLfloat* rectColor) {
    glColor3fv(diamColor);                      // set diamond color
    glBegin(GL_POLYGON);                        // draw the diamond
        glVertex2f(0.90, 0.50);
        glVertex2f(0.50, 0.90);
        glVertex2f(0.10, 0.50);
        glVertex2f(0.50, 0.10);
    glEnd();
    glColor3fv(rectColor);                      // set rectangle color
    glRectf(0.25, 0.25, 0.75, 0.75);            // draw the rectangle
}

void myDisplay(void) {                          // display callback
    cout << "MyDisplay called" << endl;
    glClearColor(0.5, 0.5, 0.5, 1.0);           // background is gray
    glClear(GL_COLOR_BUFFER_BIT);               // clear the window

    if (isReversed)                             // draw the objects
        drawObjects(BLUE_RGB, RED_RGB);
    else
        drawObjects(RED_RGB, BLUE_RGB);
    glutSwapBuffers();                          // swap buffers
}

void myTimer(int id) {                          // timer callback
    cout << "Timer just went off.  Reversing colors." << endl;
    isReversed = !isReversed;                   // reverse drawing colors
    glutPostRedisplay();                        // request redraw
    glutTimerFunc(TIMER_DELAY, myTimer, 0);     // reset timer for 10 seconds
}

void myMouse(int b, int s, int x, int y) {      // mouse click callback
    if (s == GLUT_DOWN) {
        cout << "Mouse click detected at coordinates x="
             << x << " and y=" << y << endl;
        if (b == GLUT_LEFT_BUTTON) {
            isReversed = !isReversed;
            cout << "Left mouse click.  Reversing colors." << endl;
            glutPostRedisplay();
        }
    }
}
                                                // keyboard callback
void myKeyboard(unsigned char c, int x, int y) {
    switch (c) {                                // c is the key that is hit
        case 'q':                               // 'q' means quit
            exit(0);
            break;
        default:
            cout << "Hit q to quit.  All other characters ignored" << endl;
            break;
    }
}

//-----------------------------------------------------------------------
//  Main program
//      This does all the set up for the program.  It creates the game
//      and then passes control to glut.
//-----------------------------------------------------------------------

int main(int argc, char** argv)
{
    cout <<
    "Colors swap every 10 seconds.\n"
    "Click left mouse button to swap colors.\n" <<
    "Try resizing and covering/uncovering the window.\n" <<
    "Hit q to quit." << endl;

    glutInit(&argc, argv);                      // OpenGL initializations
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);// double buffering and RGB
    glutInitWindowSize(400, 400);               // create a 400x400 window
    glutInitWindowPosition(0, 0);               // ...in the upper left
    glutCreateWindow(argv[0]);                  // create the window

    glutDisplayFunc(myDisplay);                 // setup callbacks
    glutReshapeFunc(myReshape);
    glutMouseFunc(myMouse);
    glutKeyboardFunc(myKeyboard);
    glutTimerFunc(TIMER_DELAY, myTimer, 0);
    glutMainLoop();                             // start it running
    return 0;                                   // ANSI C expects this
}

问题出在哪里?

[错误] 'glutPostRedisplay' 未在此范围内声明

[错误] 'glutSwapBuffers' 未在此范围内声明

[错误] 'glutPostRedisplay' 未在此范围内声明

[错误] 'glutTimerFunc' 未在此范围内声明

[错误] 'GLUT_DOWN' 未在此范围内声明

[错误] 'GLUT_LEFT_BUTTON' 未在此范围内声明

[错误] 'glutPostRedisplay' 未在此范围内声明

等等

您应该在该源文件中查找声明这些函数和变量的头文件以及include它们。

一个好的起点是您似乎包含 glut.h 的 GL 目录,假设它存在。

在不调用 main() 中的函数的情况下检查建筑物。然后一一尝试。我认为错误来自您的一个包含文件。

这些文件中是否定义了任何 classes。检查头文件是否正常。检查指定路径中是​​否存在头文件。

然后检查命名空间。如果您使用命名空间定义了 class,请不要忘记使用 using namespace namespace_name 或为变量指定完整的限定名称。

来自 GLUT 文档 here:

The header files for GLUT should be included in GLUT programs with the following include directive (which you have):

#include <GL/glut.h>

Because a very large window system software vendor (who will remain nameless) has an apparent inability to appreciate that OpenGL's API is independent of their window system API, portable ANSI C GLUT programs should not directly include <GL/gl.h> or <GL/glu.h>. Instead, ANSI C GLUT programs should rely on <GL/glut.h> to include the necessary OpenGL and GLU related header files.

如果您不在 windows,这可能会导致问题。

看起来确实没有正确包含此文件,因为即使是符号常量(例如 GLUT_DOWN)也没有被解析。