首页 > 解决方案 > 错误说“相机未定义”,但我确实定义了它

问题描述

我的相机功能不起作用。错误说这myCamera是未定义的,但我确实定义了它。

根据错误消息,Camera是一个未知的覆盖说明符。

在这里,我已经包含了摄像头,所以这应该没问题。

级别.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:但在这里它声称 myCamera 是未定义的。

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;

这是相机类;但是没有错误消息,所以如果这里有什么问题我不知道是什么。

标签: c++openglcamera

解决方案


您有一个循环包含依赖项。scene.h包括camera.hcamera.h包括scene.h

所以当你尝试编译camera.cpp时,预处理器首先包含camera.h. 在那里它看到了scene.h. 在scene.h它看到camera.h再次包含的,但#pragma once会阻止它再次被包含。请注意,此时,camera.h仅读取到#include "scene.h". 因此,当编译器到达时camera myCamera,类型camera是未定义的,因为相应的头文件还没有完全读取。

要解决此问题,请删除scene.hin的包含camera.h。反正你不会在那里使用它。如果您需要那里的类型,请考虑前向声明

另外,有这个:

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

没有意义。#pragma once完成与包含守卫相同的任务_SCENE_H。使用其中之一,而不是两者。


推荐阅读