1

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.

  • you need to give us a bigger part of the `scene.cpp` and `scene.h` – Raffallo Dec 09 '19 at 13:48
  • *scene.h here i have included the camera header so this should be fine* -- It won't be fine if that header is included in two or more C++ compilation units. You have a global variable that will be declared multiple times when the linker gets a hold of your modules, causing a linker error. – PaulMcKenzie Dec 09 '19 at 13:50
  • 1
    please include a [mcve]. The code you post should be enough to reproduce the error but not more (you dont have to post the trigonometric calculations if thats not relevant for the problem) – 463035818_is_not_an_ai Dec 09 '19 at 13:53
  • 1
    It appears that `scene.cpp` does not have a `#include "scene.h"`. Hard to be sure because `scene.cpp` is a excerpt instead of being a [mcve]. – Eljay Dec 09 '19 at 13:53
  • @PaulMcKenzie the only other time I included camera.h is in camera.cpp – memelord420360 Dec 09 '19 at 14:12
  • In Scene.ccp you use `myCamera` in the middle of nothing, so it obviously do not exists. You should use it inside a method of `Scene` e.g. in the constructor if you want to initialize it – Chelmy88 Dec 09 '19 at 14:13

1 Answers1

2

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.

Nico Schertler
  • 32,049
  • 4
  • 39
  • 70