0

I'm trying to define a constructor for my class ShaderProgram which takes two input arguments, both of type Shader which is another class I've defined. However, I get the error "No default constructor exists for class", which to me seems like C++ is trying to create two shader objects, when I'm really just telling C++ what types my input arguments have. I'm very new to C++ so this might just be a stupid misunderstanding. This is my implementation:

ShaderProgram.cpp

#include "ShaderProgram.h"

ShaderProgram::ShaderProgram(Shader vertex_shader, Shader fragment_shader)  // Somehow error here
{
    vertex_shader_ = vertex_shader;
    fragment_shader_ = fragment_shader;
    id_ = glCreateProgram();
    glAttachShader(id_, vertex_shader_.GetId());
    glAttachShader(id_, fragment_shader_.GetId());
    glLinkProgram(id_);

    int success;
    char info_log[512];

    glGetProgramiv(id_, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(id_, 512, NULL, info_log);
        logger.Error("Failed to link program using vertex shader at '" + vertex_shader_.GetFilepath() +
            "' and fragment shader at '" + fragment_shader_.GetFilepath() + "'");
        logger.Error(info_log);
    } else
    {
        logger.Debug("Successfully linked program");
    }
}

ShaderProgram.h

#include <glad/glad.h>
#include "Shader.h"

#pragma once
class ShaderProgram
{
private:
    unsigned int id_;
    Shader vertex_shader_;
    Shader fragment_shader_;
    Logger logger = Logger("Shader Program Logger");

public:
    ShaderProgram(Shader vertex_shader, Shader fragment_shader);
};

Shader.cpp

#include "Shader.h"


Shader::Shader(std::string filepath, int type)
{
    filepath_ = filepath;
    id_ = glCreateShader(GL_VERTEX_SHADER);
    source_ = ReadShaderFile(filepath);
    c_source_ = source_.c_str();
    glShaderSource(id_, 1, &c_source_, NULL);
    glCompileShader(id_);

    int success;
    char info_log[512];
    glGetShaderiv(id_, GL_COMPILE_STATUS, &success);

    if (!success) {
        glGetShaderInfoLog(id_, 512, NULL, info_log);
        logger.Error("Failed to compile shader '" + filepath_ + "'");
        logger.Error(info_log);
    }
    else {
        logger.Debug("Successfully compiled shader '" + filepath_ + "'");
    }
}

unsigned int Shader::GetId()
{
    return id_;
}

std::string Shader::GetFilepath()
{
    return filepath_;
}


std::string Shader::ReadShaderFile(std::string filepath)
{
    std::ifstream file(filepath.c_str());
    std::stringstream string_buffer;
    string_buffer << file.rdbuf();
    file.close();
    std::string content = string_buffer.str() + '\0';
    return content;
}

Shader.h

#include <string>
#include <fstream>
#include <sstream>
#include <glad/glad.h>
#include <iostream>
#include "../Logger.h"

#pragma once

class Shader
{
private:
    int type_;
    unsigned int id_;
    std::string source_;
    const char* c_source_;
    std::string filepath_;
    Logger logger = Logger("Shader Logger");

public:

private:
    std::string ReadShaderFile(std::string filepath);
public:
    Shader(std::string filepath, int type);
    unsigned int GetId();
    std::string GetFilepath();
};
Zorobay
  • 557
  • 1
  • 6
  • 23
  • You have to use the initialization lists to initialize your `vertex_shader_` and `fragment_shader_` in `ShaderProgram`'s constructor … There's a duplicate to link to somewhere. – ChrisMM Jun 03 '20 at 18:47
  • The compiler error explains it. You're trying to default construct objects that has no default constructor. – Taekahn Jun 03 '20 at 18:47
  • I assume ist happening when you are trying to instantiate ShaderProgram. There's an error in your main funciton somewhere which you did not post. – Irelia Jun 03 '20 at 18:48
  • Omg yeah I get it, I technically initialize them in `ShaderProgram.h`. Thanks! – Zorobay Jun 03 '20 at 18:52

0 Answers0