0

The code was working fine but after I added the void projection(float), it's giving me unqaulified-id error inside the projection function. I'm using CodeBlocks IDE, MinGW. Please help me.

This is the error I'm getting:

||=== 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)) ===|

Moreover, if I comment everything inside projection, I get this error:

 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'|

CallbackFunctions.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

CallbackFunctions.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);
}*/
WhiteSword
  • 101
  • 9
  • 1. You call 'projection' in 'reshape ()' before it being declared. 2. You have an assignment instead of the comparison in 'if' near the 'GLUT_KEY_F1'. 3. Your source code has no line numbers so it's not so easy to find the lines 56 and 57 with errors. – mbaitoff Jun 13 '17 at 16:15
  • @mbaitoff It's on `float fov = 55.0, near = asp/5, far = asp*5;` the else part in projection – WhiteSword Jun 13 '17 at 16:17
  • but why is it reporting about `multiple definitions` when I comment the code? – WhiteSword Jun 13 '17 at 16:18
  • 1
    'near' and 'far' were reserved words in C as I remember. Try to use different names. Also post your code to pastebin.com and post the link here - it will numerate the lines. Multiple definitions are reported because your function calls look like definitions because of some previous errors (commas and or brackets) – mbaitoff Jun 13 '17 at 16:21
  • https://pastebin.com/6rjb4QSY – WhiteSword Jun 13 '17 at 16:27
  • 1
    @mbaitoff: Pastebin links are not wanted on SO because the question will get useless when the pastebin gets deleted. Everything relevant for the question has to be included in the question itself. – BDL Jun 13 '17 at 16:28
  • @BDL let me know if any else info is required. – WhiteSword Jun 13 '17 at 16:29
  • The code doesn't look obviously erroneous to me right now. I have no further ideas except probably fiddle with variables' and functions' names (like renaming 'projection' to 'my_projection' to avoid possible name collisions. Also you may try to decouple the multiple declaration of far and near planes into several separate declarations. – mbaitoff Jun 14 '17 at 03:08
  • To go with what @mbaitoff said, I can compile this (with an empty Text.h): g++ -c CallbackFunctions.cpp -o CallbackFunctions with no errors and warnings about string constant. I think at this point there needs to be a [minimal, complete, and verifiable example.](https://stackoverflow.com/help/mcve) – merelyMark Jun 14 '17 at 05:40
  • @merelyMark . On my side I'm still getting the same errors. Most likely the problem is with the compiler setup. I wrote very basic program this time. I kept only one global variable, the compiler was pointing error `multiple defintion` to this variable, even if I rename the variable to anything, even my own name. So, I replaced it with a macro. And now it works fine. I don't know why the compiler isn't accepting global variable declarations. Any idea? – WhiteSword Jun 14 '17 at 06:06
  • @merelyMark This is the minimal example which I could manage to write that produces the erroneous behaviour on my laptop. Here's the pastebin [link](https://pastebin.com/CNwijg17) ..... I've also included the build log in the end of the file. – WhiteSword Jun 14 '17 at 06:58
  • @ManjinderSinghHanjra You didn't include all the code in the pastebin for your MVCE. Your compile output shows a Display.cpp being compiled and a Display.o linked to your Main.cpp. Previously, you mentioned that "I kept only one global variable" which is keyboardKey. Since you include CallBackFunctions.h in both Main.cpp and CallbakFunctions.cpp, your global keyboardKey will be in both obj files, thus the error. – merelyMark Jun 14 '17 at 07:24
  • Display header and source are empty. I forgot to remove the include. I included CallbackFunctions.h in CallbackFunctions.cpp to provide the implementation of the functions. And then in main.cpp to access CallbackFunctions functionality. – WhiteSword Jun 14 '17 at 07:32

1 Answers1

2

This is really a global variable declaration in a header problem. I've included the pastebin code (with fixes) for posterity.

/*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);
}
merelyMark
  • 367
  • 3
  • 9