Frage

i was writing a code in C/C++ and i face an error .

#include "glut.h"
#include <random>

// Classes and structs // 
struct GLPoint {
    GLfloat x, y;
};

// Method(s) Declration // 
void drawDot(GLfloat, GLfloat);
void serpinski_render(void);
void myInti(void); 

// Method(s) Implementation // 
void drawDot(GLfloat x, GLfloat y){
    glBegin(GL_POINTS);
    glVertex2i(x, y);
    glEnd();
}
void serpinski_render(void)
{
    glClear(GL_COLOR_BUFFER_BIT);  // Clear the screen from anything is displayed on it 
    GLPoint T[3] = { { 10, 10 }, { 600, 10 }, { 300, 600 } }; // the three points of parent triangle 

    int index = rand() % 3;  // this mean i will choose a random number between 0 , 3 
    GLPoint point = T[index];
    drawDot(point.x, point.y);

    for (unsigned int i = 0; i < 5500; i++) // a loop that going to run 5500 ( a very big number ) 
    {
        index = rand() % 3;
        point.x = (point.x + T[index].x) / 2;
        point.y = (point.y + T[index].y) / 2;

        drawDot(point.x, point.y); 
    }
    glFlush(); 

}
void myInti(void)
{
    glClearColor(1, 1, 1, 0);  // a white background 
    glColor3f(0, 0, 0); // black points 
    glPointSize(3); // 3 pixel point size 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(0, 640, 0, 480); 
}


// Main Method //
void main(int argc ,char ** argv )
{
    glutInit(&argc, argv); // intilize toolkit 
    glutInitWindowPosition(100, 150); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(640, 480); // windows size is 640 x 480 
    glutDisplayFunc(serpinski_render); 

    myInti(); 
    glutMainLoop(); 
}

i dont know if it will work fine but this code should produce me Sierpinski triangle . and i face every time i use C++ library in this case the random lib this problem in the stdlib.h making me confused never face something like it before

Error 1 error C2381: 'exit' : redefinition; __declspec(noreturn) differs c:\program files (x86)\microsoft visual studio 12.0\vc\include\stdlib.h 376

War es hilfreich?

Lösung

There is an incompatibility between glut.h and Visual Studio .NET, which is the usage of both "glut.h" and in your case. You can solve it by just declaring:

#include <random>
#include "glut.h"

instead of:

#include "glut.h"
#include <random>

Please read this description for further information and for an another solution. ("Header (.h) files" section)

Also your code will possibly fail because of not creating window. You can use glutCreateWindow to create a window. You can also solve this issue by arranging your main like below:

void main(int argc ,char ** argv )
{
    glutInit(&argc, argv); // intilize toolkit 
    glutInitWindowPosition(100, 150); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(640, 480); // windows size is 640 x 480 
    glutCreateWindow("A title");
    myInti(); 
    glutDisplayFunc(serpinski_render); 

    glutMainLoop(); 
}

Please also read this information for glutCreateWindow function.

Andere Tipps

You probably have this code in glut.h:

# ifndef GLUT_BUILDING_LIB
extern _CRTIMP void __cdecl exit(int);
# endif

The glut.h header is quite old. This was probably a workaround for an old VC deficiency. Visual C now seems to have a declaration that conflicts with this one. The easy solution is to just delete these lines from the header, since there is a valid definition in stdlib.h.

By the way, all the glVertex, glBegin, glEnd, matrix stack, and many other OpenGL calls are deprecated in favor of shaders.

Perhaps there is also a newer/better glut available. I'd check that out.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top