2013-02-05 6 views
0

다른 개체를 복사하여 새 클래스를 만드는 데 약간의 문제가 있습니다. 내가 볼 수있는, 아래의 코드는 작동하지만, 내 컴파일러는 클래스 생성자의 기본 생성자가 누락되었습니다. 내가 볼 수있는 것에서 이것은 필요하지 않습니다. 내가 여기서 뭐 잘못하고 있니?C++ 생성자를 통해 개체 복사

먼저 클래스 생성자 :

shadingBatch::shadingBatch(const vertexAttribLayout& layout, const material& batchMaterial){ 
    dataFormat_ = layout; 
    batchMaterial_ = *(new material(batchMaterial)); 
} 

는 또한

shadingBatch::shadingBatch(const vertexAttribLayout& layout, const material& batchMaterial){ 
      dataFormat_ = layout; 
      batchMaterial_ = batchMaterial; 
     } 

을 시도했지만 같은 컴파일러 오류가 반환됩니다.

두 번째 클래스 정의

class material { 
protected: 
    shader shader_; 
public: 
    material (const shader* shaderProgram); 
    material (const material&); 
    ~material(); 

    void compileShader(); 
} ; 

두 번째 클래스의 복사 생성자

material::material(const material& other){ 
     shader_ = *(new shader(other.shader_)); 
    } 

편집 : 요청으로

첫 번째 클래스 정의

class shadingBatch { 
    friend class cheeseRenderer; 
protected: 
    std::vector<primitive*> primitives_; 
    std::vector<vertex> vertices_; 
    std::vector<GLuint> elements_; 
    vertexAttribLayout dataFormat_; 
    material batchMaterial_; 
    GLuint VAO_; 
    GLuint VBO_; 
    GLuint EBO_; 
public: 
    ~shadingBatch(); 
    GLuint updateBatch (void); 
    void addPrimitive (primitive*); 
    shadingBatch(const vertexAttribLayout&, const material&); 
private: 
    void updatePrimitives (void); 
    void setVertexAttributes(void); 
} ; 

그리고 어디 구조를 또는 다음과 같이 호출됩니다.

shader* defaultShader = new shader(fragmentSource,vertexSource); 
material* defaultMaterial = new material(defaultShader); 
vertexAttribLayout* defaultVertexData = new vertexAttribLayout(); 
shadingBatch* batch = new shadingBatch(*defaultVertexData,*defaultMaterial); 
cheeseRenderer renderer(*batch); 
+0

오류 메시지를 추가하십시오. 기본 생성자를 놓친 경우 컴파일러가 true 인 시간은 99.999999 %입니다. 하나의 클래스가 누락 된 기본 생성자 클래스를 멤버로 가지며 클래스의 해당 멤버를 올바르게 초기화하지 않는 것이 일반적입니다. –

+0

'shadingBatch' 정의와 그 생성자에 대한 호출을 보여줄 수 있습니까? –

+3

정말 필요하지 않으면 무엇을하고 있는지 알지 못하면'new'를 사용하지 마십시오. 현재 문제와 리소스 누출의 원인이되고 있습니다. – juanchopanza

답변

1

당신이하는 일은 객체의 구성이 아니라, 이후에 내부 객체 에 인수를 복사하는 것입니다. 당신은 초기화 목록의 개념을 찾고 싶다. 이 같은 모습을 구현하려는 생성자 : 당신은 초기화 목록에 명시 적으로 클래스 멤버를 초기화하지 않는 경우

shadingBatch::shadingBatch(const vertexAttribLayout& layout, const material& batchMaterial) 
    : dataFormat_(layout), batchMaterial_(batchMaterial) // <-- initialization list 
{} 

, 그들은 건설 기본값이됩니다. 또는 최소한 컴파일러가 시도합니다. 재질은 기본 생성자가없는 것처럼 보이므로 컴파일러는 두 시도 모두에서 불평합니다.

Sidenote : 첫 번째 생성자 시도가 new을 통해 개체를 만들고 결과 포인터를 저장하거나 삭제하지 않으므로 다른 메모리 누수가 발생합니다. 메모리와 개체 자체는 영원히 손실됩니다.

4

우선 이니셜 라이저 목록을 사용해야합니다. 이니셜 라이저 목록에서 명시 적으로 초기화되지 않은 멤버 변수 객체는 생성자 본문이 실행되기 전에 호출 된 기본 생성자를 갖습니다. 그런 다음 새로 작성하지만 삭제하지 마십시오 (포인터를 잃어 버리기 때문에). 생성자가 메모리를 누설합니다.

shadingBatch::shadingBatch(const vertexAttribLayout& layout, 
          const material& batchMaterial) : 
    dataFormat_(layout) 
    ,batchMaterial_(batchMaterial) 
{ 
} 

은 위 dataFormat_batchMaterial_ 모두 클래스 shadingBatch의 멤버 변수, 그리고 그들이 클래스 정의의 순서로 선언되어 있다고 가정합니다, 그래서 이것은 너무하지 않은 경우 필요에 따라 수정이를보십시오.


귀하의 material::material 당신이 뭔가를 필요로하므로 또한, 메모리 누수 및 기본 생성자 모두 문제가있다 :

material::material(const material& other) : shader_(other.shader_) {} 

(그리고 거기 할 더 많은,하지만 당신은 아이디어를 얻을 경우 가능성이 더 초기화 코드 .)

관련 문제