gpt4 book ai didi

C++ 编译器抛出 GLUT 错误

转载 作者:行者123 更新时间:2023-11-28 01:47:17 26 4
gpt4 key购买 nike

代码运行良好,但在我添加了 void projection(float) 之后,它在 projection 中出现了 unqaulified-id 错误功能。我正在使用 CodeBlocks IDE,MinGW。请帮助我。

这是我遇到的错误:

||=== Build: Debug in L3 (compiler: GNU GCC Compiler) ===|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp||In function 'void projection(float)':|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp|56|error: expected unqualified-id before '=' token|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp|57|error: expected primary-expression before ',' token|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp| 57|error: expected primary-expression before ')' token|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp||In function 'void specialKeyboard(int, int, int)':|

||=== Build failed: 3 error(s), 15 warning(s) (0 minute(s), 0 second(s)) ===|

此外,如果我在 projection 中注释所有内容,我会收到此错误:

 obj\Debug\src\Display.o||In function `glutInit_ATEXIT_HACK':| 
C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple definition of `keyboardKey'|
C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple definition of `mouseX'|
C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple definition of `mouseY'|
C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple definition of `toggleProjection'|

回调函数.h

#ifndef CALLBACKFUNCTIONS_H
#define CALLBACKFUNCTIONS_H

#include "GL/freeglut.h"


unsigned char keyboardKey;
char *specialKeyboardKey ="";
char *modifier="";

char *mouseButtonPressed="";
char *mouseState="";
int mouseX, mouseY;

int toggleProjection=0;

char *view="";




/* Hardware Interrupts callback functions */
void display(); //called every single frame
void reshape(int width, int height); //called whenever the dimensions of the window are changed
void idle(); //when there is no interaction with the window
void keyboard(unsigned char key, int x, int y); //whenever a keyboard key is pressed/interrupt occurs
void specialKeyboard(int button, int x, int y); //captures special keys like: SHIFT ALT CTRL F1.....
void mouse(int button, int state, int x, int y); //captures the mouse clicks and their state
void mouseMotion(int x, int y); //captures the motion of the mouse pointer in the window


void projection(float asp);

#endif // CALLBACKFUNCTIONS_H

回调函数.cpp

#include "../include/CallbackFunctions.h"
#include "../include/Text.h"

extern int dim;



void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);

//Drawing stuff

glFlush();
glutSwapBuffers();
}

void idle()
{
display();
}

/* This callback function is called whenever the window is resized or reshaped */
void reshape(int width, int height)
{
//Width To Height Ratio
float w2h = (height>0)? (float)width/height : 1;
glViewport(0, 0, width, height);
projection(w2h);
}


void projection(float asp)
{
//Setting up the Viewing Matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(toggleProjection==0)
{
glOrtho(-dim*asp, +dim*asp, -dim, +dim, -dim, +dim);
}
else
{
float fov = 55.0, near = asp/5, far = asp*5;
gluPerspective(fov, asp, near, far);
//To do
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

/* This callback function is called when there is no activity in the window */
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
}
keyboardKey = key;
glutPostRedisplay();
}


/* This callback function is called whenever a keyboard interrupt is encountered */
void specialKeyboard(int key, int state, int a)
{

int currentModifier = glutGetModifiers();
switch(currentModifier)
{
case GLUT_ACTIVE_SHIFT:
modifier = "SHIFT";
break;
case GLUT_ACTIVE_CTRL:
modifier = "CTRL";
break;
case GLUT_ACTIVE_ALT:
modifier = "ALT";
break;
}
switch(key)
{
case GLUT_KEY_F1:
if(toggleProjection=0)
{
toggleProjection = 1;
view = "perspective";
break;
}
else
{
toggleProjection = 0;
view = "orthographic";
break;
}
}
glutPostRedisplay();
}


void mouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON)
mouseButtonPressed = "Left";
else if(button == GLUT_RIGHT_BUTTON)
mouseButtonPressed = "Right";

if(state == GLUT_DOWN)
mouseState = "down";
else if(state == GLUT_UP)
mouseState = "up";
}

void mouseMotion(int x, int y)
{
mouseX = x;
mouseY = y;
}



/*void info()
{
displayTextAt(-130, -40, 0, "mouseButton: %s", mouseButtonPressed);
displayTextAt(-130, -50, 0, "mouseButtonState: %s", mouseState);
displayTextAt(-130, -60, 0, "mouseX: %d", mouseX);
displayTextAt(-130, -70, 0, "mouseY: %d", mouseY);
displayTextAt(-130, -80, 0, "keyboard key: %c", keyboardKey);
displayTextAt(-130, -90, 0, "modifier: %s", modifier);
displayTextAt(70, -90, 0, view);
}*/

最佳答案

这真的是a global variable declaration in a header problem.我为后代包含了 pastebin 代码(带有修复)。

/*This is the CallbackFunctions.h ------------------ */
#ifndef CALLBACKFUNCTIONS_H
#define CALLBACKFUNCTIONS_H
#include <GL/freeglut.h>

extern unsigned char keyboardKey;


/* Hardware Interrupts callback functions */
void display(); //called every single frame
void keyboard(unsigned char key, int x, int y); //whenever a keyboard key is pressed/interrupt occurs
#endif // CALLBACKFUNCTIONS_H

/*This is the CallbackFunctions.cpp ------------------ */

#include "CallbackFunctions.h"



unsigned char keyboardKey = 0;

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void keyboard(unsigned char key, int x, int y)
{
keyboardKey = key;
}



/* --------------- This is the Main.cpp ---------------- */

#include <GL/freeglut.h>
#include "CallbackFunctions.h"



#define WIDTH 700
#define HEIGHT 400

void setupGlutWindow(int *argc, char **argv, int width, int height);
void setupCallbackFunctions();

int main(int argc, char** argv)
{
setupGlutWindow(&argc, argv, WIDTH, HEIGHT);
setupCallbackFunctions();
glutMainLoop();
}

void setupGlutWindow(int *argc, char **argv, int width, int height)
{
glutInit(argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(700, 500);
glutCreateWindow("Project L3");
glViewport(0, 0, width, height);
}

void setupCallbackFunctions()
{
glutDisplayFunc(display);
glutIdleFunc(display);
glutKeyboardFunc(keyboard);
}

关于C++ 编译器抛出 GLUT 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44525994/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com