c++ - 为什么 exit(0) 给我错误?

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

所以我一直在学习教程,当我尝试编译以下代码时:

#include <glut.h>
#include <iostream>

void render(void);
void keyboard(unsigned char c, int x, int y);
void mouse(int button, int state, int x, int y);

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(640, 480);
    glutCreateWindow("Test GLUT App");

    glutDisplayFunc(render); // render
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);

    glutMainLoop(); // initialization finished. start rendering
}

void render(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
    glColor3f(0.5, 0.2, 0.9);
    glVertex2f(-0.5, -0.5);
    glColor3f(0.1, 0.2, 0.5);
    glVertex2f(0.0, -0.5);
    glColor3f(0.3, 0.9, 0.7);
    glVertex2f(0.0, 0.5);
    glEnd();

    glutSwapBuffers();  
}

void keyboard(unsigned char c, int x, int y)
{
    if(c == 27)
    {
        exit(0);
    }
}

void mouse(int button, int state, int x, int y)
{
    if(button == GLUT_RIGHT_BUTTON)
    {
        exit(0);
    }
}

我不知从哪里得到了 3 个错误:

错误 1 ​​错误 C2381:“退出”:重新定义; __declspec(noreturn) 不同于 c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdlib.h 353

错误 2 error C3861: 'exit': identifier not found ....main.cpp 45

错误 3 error C3861: 'exit': identifier not found ....main.cpp 53

有人知道为什么会出现这个错误吗?我正在使用 VS2010。

最佳答案

您需要 #include <cstdlib> .

编辑:

您可能正在学习一个非常有名的教程,该教程为您提供了一个头文件。

这会帮助你GLUT exit redefinition error

关于c++ - 为什么 exit(0) 给我错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11607726/

相关文章:

c++ - 在 Arduino IDE 中链接预建对象

wpf - 无法创建事件处理程序 WPF

c++ - 将 FILE * 转换为 CMemFile(或从 FILE * 检索 void*)

c++ - 如何在 C++ 中的 ifstream 函数生成的文件中搜索特定单词

c++ - std::forward_list 中可以存在循环吗?

c++ - 使用 ImageMagick 加载 bmp

c++ - 未在 Windows 8.1 上构建的 Node 包 - 缺少 Microsoft.Cpp.Default.props

C# 将 Conditional() 与环境变量一起使用

c++ - 错误 C2601 : 'main' : local function definitions are illegall - MS VS 2013 Compiler

c++ - 初始化程序列表不适用于 Visual Studio 2012 中的 vector ?