I have a class defined in a header and properly implemented(I think), and that is properly included(I think) wherever I try to use it.
I think I am missing something in regards to some basic syntax surrounding class definition.
I have read pretty much every C++ "undefined reference to class constructor" question on stackoverflow, but none of them seem to apply to my situation.
main.cpp
#include "headers/init.h"
int main()
{
GameInstance *game = new GameInstance(60,480,480,"window");
}
init.cpp
#include "../headers/init.h"
...
Shader vertex("../shaders/vertex.glsl", GL_VERTEX_SHADER);
...
shader.h
#ifndef SHADER_H
#define SHADER_H
#include <string>
#include <iostream>
#include <GL/glew.h>
#include <GL/gl.h>
#include <fstream>
class Shader
{
std::string sh_contents;
std::ifstream sh_source;
GLenum sh_type;
GLuint sh_id;
public:
Shader(const char path[], GLenum p_type );
void compile(GLsizei p_count);
char* geterror();
};
#endif // SHADER_H
shader.cpp
#include "../headers/shader.h"
Shader::Shader(const char path[], GLenum p_type)
{
sh_type = p_type;
sh_source.open(path);
if(sh_source.is_open())
{
sh_source >> sh_contents;
}
else
{
std::cout << "Shader error: couldn't read from file specified!";
}
}
void Shader::compile(GLsizei p_count)
{
sh_id = glCreateShader(sh_type);
const char * buffer = sh_contents.c_str();
int length = sh_contents.length();
glShaderSource(sh_id, p_count, &buffer, &length);
glCompileShader(sh_id);
}
char* Shader::geterror()
{
if(glIsShader(sh_id))
{
GLint sh_status;
glGetShaderiv(sh_id,GL_COMPILE_STATUS, &sh_status);
if(sh_status == GL_FALSE)
{
GLint er_length;
glGetShaderiv(sh_id, GL_INFO_LOG_LENGTH,&er_length);
char* er_return;
glGetShaderInfoLog(sh_id, er_length, &er_length, er_return);
glDeleteShader(sh_id);
return er_return;
}
else
{
char* er_return = '\0';
return er_return;
}
}
}
Edit #1:
The error:
/home/dok/SDL2/ngin/engine/source/init.cpp|71|undefined reference to `Shader::Shader(char*, unsigned int)'|
Edit #2:
I was able to compile the source with gpp
via a BASH terminal, and it compiled effortlessly(note: I have updated the code as per many answerers' suggestion), so it seems this is a Code::Blocks issue.
This is the command I used to compile my program with gpp: g++ main.cpp source/init.cpp source/shader.cpp -lSDL2 -lGLEW -lGL
This is my CodeBlocksProject file:
ngin.cpb
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="engine" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/engine" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
<Linker>
<Add library="SDL2" />
<Add library="GLEW" />
<Add library="GL" />
</Linker>
</Target>
<Target title="Release">
<Option output="bin/Release/engine" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="headers/init.h" />
<Unit filename="headers/shader.h" />
<Unit filename="main.cpp" />
<Unit filename="shaders/fragment.glsl" />
<Unit filename="shaders/vertex.glsl" />
<Unit filename="source/init.cpp" />
<Unit filename="source/shader.cpp">
<Option target="<{~None~}>" />
</Unit>
<Extensions>
<code_completion />
<envvars />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>