Camera未定义报错排查:已定义仍提示myCamera未定义等问题
Let's break down your issues one by one—there are a few straightforward mistakes in your code that are causing these errors:
1. Circular Header Inclusion in level.h
In your
level.hfile, you've included the file itself with#include "level.h". This creates a circular inclusion loop that confuses the compiler and prevents it from properly parsing your class definitions. Delete this line immediately.
2. Mismatched Constructor Name vs. Class Name
Your class is named Scene, but you declared its constructor as level(Input *in);—this is a critical mismatch! The .cpp file's level::level definition can't find a corresponding class, which is why the compiler doesn't recognize myCamera (it doesn't know which class that member belongs to).
- Fix the constructor declaration in
level.hto match the class name:Scene(Input *in); - Update the constructor definition in
level.cppto match:Scene::Scene(Input *in) { // Your existing code here }
3. Case Sensitivity Issue with the Camera Class
The error "Camera是未知覆盖说明符" (Camera is an unknown override specifier) almost certainly comes from a case mismatch between your class declaration and usage:
- If your
camera.hdefines a class namedCamera(uppercase C), you need to declare your member variable asCamera myCamera;(not lowercasecamera myCamera;) in theSceneclass. - Double-check
camera.hto confirm the exact class name spelling and case—C++ is case-sensitive, soCameraandcameraare treated as completely different types.
4. Incorrect Access to the Camera Instance
In your .cpp constructor, you wrote camera.position.x = 0;—this tries to access a static member of the camera class, but position is a member of your myCamera instance. Fix this to:
myCamera.position.x = 0;
5. Uninitialized width/height Variables
You're using width and height in gluPerspective before initializing them. This will cause invalid perspective matrix calculations. Add initial values in the constructor, e.g.:
width = 800; // Or pass these values in via the constructor height = 600;
After fixing all these issues, clean your build directory and recompile—both of your errors should be resolved.
内容的提问来源于stack exchange,提问作者memelord420360

