我正在尝试为以下教程编写临时渲染器
this site。我有两个类SceneObject和RenderComponent。一个SceneObject应该包含一个RenderComponent,然后再绘制SceneObject。这是代码:
SceneObject.h
#ifndef _SCENE_OBJECT_H
#define _SCENE_OBJECT_H
#include <GLM/glm.hpp>
#include <GLM/gtc/matrix_transform.hpp>
#include <GLM/gtc/type_ptr.hpp>
#include <vector>
#include <iostream>
#include "..\headers\shader.h"
#include "..\headers\rendercomponent.h"
class SceneObject {
private:
glm::vec3 position;
float *vertices;
RenderComponent* renderComponent;
std::vector<Shader> shaders;
public:
SceneObject(glm::vec3, GLfloat*);
~SceneObject();
bool init();
RenderComponent& getRenderComponent() const;
};
#endif
SceneObject.cpp
#include "..\headers\sceneobject.h"
SceneObject::SceneObject(glm::vec3 pos, GLfloat* objectVertices) {
this->position = pos;
this->vertices = objectVertices;
renderComponent = new RenderComponent(*this);
}
SceneObject::~SceneObject() {}
RenderComponent& SceneObject::getRenderComponent() const {
return *renderComponent;
}
bool SceneObject::init() {
if (!renderComponent->initialize()) {
return false;
}
}
RenderComponent.h
#ifndef _RENDER_COMPONENT_H
#define _RENDER_COMPONENT_H
#include <GLM/glm.hpp>
#include <GLM/gtc/matrix_transform.hpp>
#include <GLM/gtc/type_ptr.hpp>
#include <iostream>
#include "../headers/sceneobject.h"
class RenderComponent {
SceneObject &sceneObject;
public:
RenderComponent(SceneObject&);
RenderComponent(const RenderComponent&);
bool initialize();
void draw();
SceneObject& getSceneObject() const;
};
#endif
RenderComponent.cpp
#include "..\headers\rendercomponent.h"
RenderComponent::RenderComponent(SceneObject& obj)
:sceneObject(obj){}
RenderComponent::RenderComponent(const RenderComponent& ref)
: sceneObject(ref.getSceneObject()){}
bool RenderComponent::initialize() {}
void RenderComponent::draw() {}
SceneObject& RenderComponent::getSceneObject() const {
return sceneObject;
}
产生以下错误。
Error C2143 syntax error: missing ';' before '&'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';' syntax error: identifier 'SceneObject'
Error C2143 syntax error: missing ';' before '&'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
Error C2664 'RenderComponent::RenderComponent(const RenderComponent &)': cannot convert argument 1 from 'SceneObject' to 'const RenderComponent &'
Error C2143 syntax error: missing ';' before '&'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
Error C2061 syntax error: identifier 'SceneObject'
Error C2143 syntax error: missing ';' before '&'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
Error C2143 syntax error: missing ';' before '*'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
Error C2143 syntax error: missing ';' before '&'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
据我了解,编译器在说SceneObject不是类型。我认为这是问题所在。任何帮助表示赞赏。
最佳答案
您有包括sceneobject.h
的rendercomponent.h
和包括rendercomponent.h
的sceneobject.h
。使用include防护时,其中一个不会知道另一个 header 中定义的类。
从一个或两个 header 中删除包含,然后向前声明该类。