No default constructor exists for class "Shader"

Hello, I am having an issue with one class, which has been giving me this error: "E0291 no default constructor exists for class "Shader""

I am confused because there's no class named Shader in this file and the error shows on line 4 of ShaderProgram.cpp


ShaderProgram.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include "Shader.h"

class ShaderProgram
{
private:
	GLuint ID;

	Shader fragmentShader;
	Shader vertexShader;

	void CheckCompileErrors();

public:
	ShaderProgram(const char* vertShaderPath, const char* fragShaderPath);
	~ShaderProgram();

	void Use();
};


ShaderProgram.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "ShaderProgram.h"

ShaderProgram::ShaderProgram(const char* vertShaderPath, const char* fragShaderPath)
{
	fragmentShader = Shader(fragShaderPath, GL_FRAGMENT_SHADER);
	vertexShader = Shader(vertShaderPath, GL_VERTEX_SHADER);

	ID = glCreateProgram();
	fragmentShader.CompileShader();
	vertexShader.CompileShader();

	glAttachShader(ID, fragmentShader.GetID());
	glAttachShader(ID, vertexShader.GetID());
	glLinkProgram(ID);

	fragmentShader.~Shader();
	vertexShader.~Shader();

	CheckCompileErrors();
}

ShaderProgram::~ShaderProgram()
{
	glDeleteProgram(ID);
}

void ShaderProgram::Use()
{
	glUseProgram(ID);
}

void ShaderProgram::CheckCompileErrors()
{
	GLchar infoLog[1024];
	int result;

	glGetProgramiv(ID, GL_LINK_STATUS, &result);

	if (!result)
	{
		glGetProgramInfoLog(ID, 1024, NULL, infoLog);
		std::cout << "ERROR: UNABLE TO LINK SHADERS: " << "\n" << infoLog << std::endl;
	}

	return;
}
Last edited on
> no class named Shader in this file

There are member objects of type Shader; these require initialisation.
Use a member initializer list.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "ShaderProgram.h"

ShaderProgram::ShaderProgram(const char* vertShaderPath, const char* fragShaderPath)
    : fragmentShader(fragShaderPath, GL_FRAGMENT_SHADER), 
      vertexShader(vertShaderPath, GL_VERTEX_SHADER)
{
	// fragmentShader = Shader(fragShaderPath, GL_FRAGMENT_SHADER);
	// vertexShader = Shader(vertShaderPath, GL_VERTEX_SHADER);

	ID = glCreateProgram();
	
        fragmentShader.CompileShader();
        // etc ... 
Ah, it worked! Thank you very much, I was about to give up on this.
Topic archived. No new replies allowed.