memelord420360
memelord420360

Reputation: 35

Error says "camera is undefined", but I did define it

My camera function is not working. The error says that myCamera is undefined, but I did define it.

According to the error message, Camera is an unknown override specifier.

Here I have included the camera header so this should be fine.

level.h:


#pragma once
#include "Vectors.h"
#include "level.h"



#include "glut.h"
#include <gl/GL.h>
#include <gl/GLU.h>
#include "controls.h"
#include <stdio.h>
#include "SOIL.h"
#include <vector>
#include "camera.h"
class Scene{

public:
    level(Input *in);

    void renderer();

    void handleInput(float dt);

    void update(float dt);

    void resize(int w, int h);

protected:

    void displayText(float x, float y, float r, float g, float b, char* string);

    void renderTextOutput();
    void calculateFPS();



    Input* input;


    int width, height;
    float fov, nearPlane, farPlane;


    int frame = 0, time, timebase = 0;
    camera myCamera;
};


level.cpp: yet here it claims myCamera is undefined.

level::level(Input *in)
{
    // Store pointer for input class
    input = in;

    //OpenGL settings
    glShadeModel(GL_SMOOTH);                            
    glClearColor(0.39f, 0.58f, 93.0f, 1.0f);            
    glClearDepth(1.0f);                                     glClearStencil(0);                                  
    glEnable(GL_DEPTH_TEST);                            
    glDepthFunc(GL_LEQUAL);                             
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  
    glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
    glEnable(GL_TEXTURE_2D);




    gluPerspective(55.0f, (GLfloat)width / (GLfloat)height, 1, 50.0f);


    camera.position.x = 0;

Here is the camera class; however there are no error messages, so if anything is wrong here I don't know what.

Upvotes: 1

Views: 447

Answers (1)

Nico Schertler
Nico Schertler

Reputation: 32627

You have a cyclic include dependency. scene.h includes camera.h and camera.h includes scene.h.

So when you try to compile camera.cpp, the preprocessor first includes camera.h. In there it sees the include of scene.h. In scene.h it sees the include of camera.h again, but the #pragma once will prevent it from being included again. Note that at that point, camera.h has only been read up until #include "scene.h". Therefore, when the compiler gets to camera myCamera, the type camera is undefined because the corresponding header file is not yet read completely.

To resolve this, remove the include of scene.h in camera.h. You don't use it there anyway. If you need types from there, consider a forward declaration.

Also, having this:

#pragma once
...
#ifndef _SCENE_H
#define _SCENE_H

does not make sense. The #pragma once fulfills the same task as the include guard _SCENE_H. Use one of them, not both.

Upvotes: 2

Related Questions