c++ - 错误说“相机未定义”,但我确实定义了它
问题描述
我的相机功能不起作用。错误说这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;
这是相机类;但是没有错误消息,所以如果这里有什么问题我不知道是什么。
解决方案
您有一个循环包含依赖项。scene.h
包括camera.h
和camera.h
包括scene.h
。
所以当你尝试编译camera.cpp
时,预处理器首先包含camera.h
. 在那里它看到了scene.h
. 在scene.h
它看到camera.h
再次包含的,但#pragma once
会阻止它再次被包含。请注意,此时,camera.h
仅读取到#include "scene.h"
. 因此,当编译器到达时camera myCamera
,类型camera
是未定义的,因为相应的头文件还没有完全读取。
要解决此问题,请删除scene.h
in的包含camera.h
。反正你不会在那里使用它。如果您需要那里的类型,请考虑前向声明。
另外,有这个:
#pragma once
...
#ifndef _SCENE_H
#define _SCENE_H
没有意义。#pragma once
完成与包含守卫相同的任务_SCENE_H
。使用其中之一,而不是两者。
推荐阅读
- go - 关机后 Gorilla mux 重用/重新启动服务器
- swift - 从 firestore 分配用户名值
- c - Defining constant in c with const keyword
- javascript - 有没有更漂亮的方法来捕获 JSON 数组块上的错误?
- django-models - 在表“app_job”上插入或更新违反了外键约束“app_job_user_id”详细信息:表“accounts_user”中不存在键(user_id)=(1)
- c - 如何使用具有显式子 pid 的多个 waitpid() 作为特定进程执行顺序的参数
- macos - 程序集——macOS上的`.text`和`.section __TEXT`之间的区别?
- php - 获取 Query_String 被截断 - Apache access.log 具有完整字符串,error.log 显示截断符号 QUERY_STRING 值
- spring - Spring Cloud Gateway RouteLocator Java 代码无法获取特定路由 id
- javascript - 如何用图像替换“返回顶部”按钮并让它滑入/滑出页面而不是淡入/淡出