I am having a few problems with creating a new class by copying other objects to it. From what i can see, the code below works, however my compiler states it’s missing a default constructor of the class material. From what i can see, this is not needed. Am i doing something wrong here?
First class constructor:
shadingBatch::shadingBatch(const vertexAttribLayout& layout, const material& batchMaterial){
dataFormat_ = layout;
batchMaterial_ = *(new material(batchMaterial));
}
I have also tried
shadingBatch::shadingBatch(const vertexAttribLayout& layout, const material& batchMaterial){
dataFormat_ = layout;
batchMaterial_ = batchMaterial;
}
But the same compiler error is returned.
Second class definition
class material {
protected:
shader shader_;
public:
material (const shader* shaderProgram);
material (const material&);
~material();
void compileShader();
} ;
Second class copy constructor
material::material(const material& other){
shader_ = *(new shader(other.shader_));
}
Edit: As requested,
First class definition
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);
} ;
And where the constructor is called:
shader* defaultShader = new shader(fragmentSource,vertexSource);
material* defaultMaterial = new material(defaultShader);
vertexAttribLayout* defaultVertexData = new vertexAttribLayout();
shadingBatch* batch = new shadingBatch(*defaultVertexData,*defaultMaterial);
cheeseRenderer renderer(*batch);
What you are doing is not the construction of the objects, but copying the arguments into the internal objects after they have been constructed. You want to look up the concept of initialization lists. The constructor you want to implement looks like this:
If you do not initialize you class members explicitly in the initialization list, they will be default constructed. Or at least, the compiler will try. Material seems not to have a default constructor, so the compiler complains in both your attempts.
Sidenote: the first constructor attempt has another error, a memory leak, since you create an object via
newand neither store nor delete the resulting pointer, its memory (and the object itself) will be lost forever.