正确定义类构造函数的未定义引用?

时间:2014-07-14 17:24:02

标签: c++ oop opengl linker codeblocks

我在标题中定义了一个类并且已经正确实现(我认为),并且无论我在哪里尝试使用它,都会正确包含(我认为)。

我认为我遗漏了一些关于类定义的基本语法。

我已经在stackoverflow上阅读了几乎每个C ++“对类构造函数的未定义引用”的问题,但它们似乎都不适用于我的情况。

的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;
    }
  }
}

编辑#1:

错误:

/home/dok/SDL2/ngin/engine/source/init.cpp|71|undefined reference to `Shader::Shader(char*, unsigned int)'|

编辑#2:

我能够通过BASH终端用gpp编译源代码,并且它可以毫不费力地编译(注意:我根据许多回复者的建议更新了代码),所以看起来这是一个Code ::阻止问题。

这是我用gpp编译程序的命令:g++ main.cpp source/init.cpp source/shader.cpp -lSDL2 -lGLEW -lGL

这是我的CodeBlocksProject文件:

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="&lt;{~None~}&gt;" />
        </Unit>
        <Extensions>
            <code_completion />
            <envvars />
            <debugger />
        </Extensions>
    </Project>
</CodeBlocks_project_file>

1 个答案:

答案 0 :(得分:1)

您将构造函数声明为

Shader(char* path, GLenum p_type);

虽然错误消息显示您正在使用声明为

的构造函数
Shader(char*, unsigned int);

这是第二个参数不一致,并且没有从类型unsigned int到类型GLenum的隐式转换。

我不知道类型GLenum是如何定义的,但您可以尝试按以下方式调用构造函数

Shader vertex("../shaders/vertex.glsl", static_cast<GLenum>( GL_VERTEX_SHADER) );

使用从unsigned int类型到GLenum

类型的显式转换

还要考虑到您使用字符串文字作为第一个参数。它的类型为const char []。所以它可能不会被用作参数。您应该将第一个参数声明为const char *

此外,您还有递归包含标头。标题shader.h包含init.hinit.h包含shader.h