当前位置:  开发笔记 > 编程语言 > 正文

为什么这段代码没有编译

如何解决《为什么这段代码没有编译》经验,为你挑选了1个好方法。

我正在尝试为本网站上的教程编写一个临时渲染器 .我有两个类SceneObject和RenderComponent.SceneObject应该包含一个RenderComponent,然后应该绘制SceneObject.这是代码:

SceneObject.h

#ifndef _SCENE_OBJECT_H
#define _SCENE_OBJECT_H

#include 
#include 
#include 
#include 
#include 

#include "..\headers\shader.h"
#include "..\headers\rendercomponent.h"

class SceneObject {
private:
    glm::vec3 position;
    float *vertices;
    RenderComponent* renderComponent;
    std::vector 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 
#include 
#include 
#include 

#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不是一个类型.我认为这就是问题所在.任何帮助表示赞赏.



1> 1201ProgramA..:

你还sceneobject.h包括rendercomponent.hrendercomponent.h包括sceneobject.h.使用包含保护,其中一个不知道另一个头中定义的类.

从一个或两个标题中删除包含,然后转发声明类.


到目前为止,我忘记了我对C++不喜欢的东西.
推荐阅读
小白也坚强_177
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有