【发布时间】:2017-06-13 15:40:24
【问题描述】:
代码运行良好,但在我添加了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);
}*/
【问题讨论】:
-
1.在声明之前,您在“reshape ()”中调用“projection”。 2. 'GLUT_KEY_F1' 附近的'if' 中有一个赋值而不是比较。 3.你的源代码没有行号,所以不容易找到第56行和第57行有错误。
-
@mbaitoff 它在
float fov = 55.0, near = asp/5, far = asp*5;投影中的 else 部分 -
但是当我评论代码时为什么它报告
multiple definitions? -
我记得 'near' 和 'far' 是 C 中的保留字。尝试使用不同的名称。还将您的代码发布到 pastebin.com 并在此处发布链接 - 它会计算这些行。报告了多个定义,因为您的函数调用看起来像定义,因为之前的一些错误(逗号和/或括号)
-
@mbaitoff:在 SO 上不需要 Pastebin 链接,因为当 pastebin 被删除时,问题将变得毫无用处。与问题相关的所有内容都必须包含在问题本身中。