缺少v表。原因是什么?

时间:2016-04-01 07:27:24

标签: c++ linker vtable

我似乎无法理解为什么会收到以下错误:

Undefined symbols for architecture x86_64:
"vtable for Mesh_Box_Line", referenced from:
      Mesh_Box_Line::Mesh_Box_Line() in playground.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

以下是相关代码:

#ifndef MESH_HPP
#define MESH_HPP

class Mesh
{


public:
  virtual void
  init(const char* texturePath) = 0;

  virtual void
  render(const glm::mat4 & mvp) const = 0;

  virtual void
  cleanup() = 0;
};

#endif

现在是链接器错误正在讨论的.hpp文件

#ifndef MESH_BOX_LINE_HPP
#define MESH_BOX_LINE_HPP

#include <vector>
#include <cstring>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include <GL/glew.h>

#include "Mesh.hpp"

// Note that this is the base mesh that all boxes will translate into their representations
class Mesh_Box_Line : public Mesh
{
public:
  Mesh_Box_Line(){};
  ~Mesh_Box_Line(){};

  void
  init(const char* texturePath) override;

  void
  render(const glm::mat4 & mvp) const override;

  void
  cleanup() override;


private:

  // static data for out base mesh
  static const GLfloat g_position_buffer_data[];
  static const GLfloat g_color_buffer_data[];

  // static data for base mesh again
  // Globals for our buffer and shader ID
  GLuint PositionBufferID;  // gl generated vert buffer id
  GLuint UVBufferID;      // gl generated UV texture coord id
  GLuint TextureID;       // gl generated Texture id
  GLuint ProgramID;       // gl generated shader program id
  GLuint mvpID;           // uniform mvp
};


#endif

最后,实施

#include "Mesh_Box_Line.hpp"

#include "texture.hpp"
#include "shader.hpp"



const GLfloat Mesh_Box_Line::g_position_buffer_data[] = {
  -1.0f,-1.0f,-1.0f, // line 01
  +1.0f,-1.0f,-1.0f,
  -1.0f,-1.0f,-1.0f, // line 02
  -1.0f,+1.0f,-1.0f,
  -1.0f,-1.0f,-1.0f, // line 03
  -1.0f,-1.0f,+1.0f,

  -1.0f,+1.0f,+1.0f, // line 04
  +1.0f,+1.0f,+1.0f,
  -1.0f,+1.0f,+1.0f, // line 05
  -1.0f,-1.0f,+1.0f,
  -1.0f,+1.0f,+1.0f, // line 06
  -1.0f,+1.0f,-1.0f,

  +1.0f,-1.0f,+1.0f, // line 07
  -1.0f,-1.0f,+1.0f,
  +1.0f,-1.0f,+1.0f, // line 08
  +1.0f,+1.0f,+1.0f,
  +1.0f,-1.0f,+1.0f, // line 09
  +1.0f,-1.0f,-1.0f,

  +1.0f,+1.0f,-1.0f, // line 10
  -1.0f,+1.0f,-1.0f,
  +1.0f,+1.0f,-1.0f, // line 11
  +1.0f,-1.0f,-1.0f,
  +1.0f,+1.0f,-1.0f, // line 12
  +1.0f,+1.0f,+1.0f,

};

const GLfloat Mesh_Box_Line::g_color_buffer_data[] = {
  +0.0f,+1.0f,+1.0f, // line 01
  +0.0f,+1.0f,+1.0f,
  +0.0f,+1.0f,+1.0f,// line 02
  +0.0f,+1.0f,+1.0f,
  +0.0f,+1.0f,+1.0f,// line 03
  +0.0f,+1.0f,+1.0f,

  +0.0f,+1.0f,+1.0f,// line 04
  +0.0f,+1.0f,+1.0f,
  +0.0f,+1.0f,+1.0f,// line 05
  +0.0f,+1.0f,+1.0f,
  +0.0f,+1.0f,+1.0f,// line 06
  +0.0f,+1.0f,+1.0f,

  +0.0f,+1.0f,+1.0f,// line 07
  +0.0f,+1.0f,+1.0f,
  +0.0f,+1.0f,+1.0f,// line 08
  +0.0f,+1.0f,+1.0f,
  +0.0f,+1.0f,+1.0f,// line 09
  +0.0f,+1.0f,+1.0f,

  +0.0f,+1.0f,+1.0f,// line 10
  +0.0f,+1.0f,+1.0f,
  +0.0f,+1.0f,+1.0f,// line 11
  +0.0f,+1.0f,+1.0f,
  +0.0f,+1.0f,+1.0f,// line 12
  +0.0f,+1.0f,+1.0f,

};


void
Mesh_Box_Line::init(const char* texturePath)
{

  // Generate Buffers
  glGenBuffers(1, &PositionBufferID);  // gl generated vert buffer id
  glGenBuffers(1, &UVBufferID);      // gl generated UV texture coord id
  // Initialize & generate textures
  //TextureID = loadDDS(texturePath);

  // Load and initialize shaders
  ProgramID = LoadShaders("Simple_VS.glsl", "Simple_FS.glsl");       // gl generated shader program id
  //UniformID = glGetUniformLocation(UniformID, <var-name-in-shader>);
  glGetUniformLocation(ProgramID, "mvp");
}

void
Mesh_Box_Line::render(const glm::mat4 & mvp) const
{
  // BIND SHADER
  glUseProgram(ProgramID);
  // BIND UNIFORM DATA
  glUniformMatrix4fv(mvpID, 1, GL_FALSE, &mvp[0][0]);
  // BIND TEXTURE
  //  glActiveTexture(GL_TEXTURE0);
  //  glBindTexture(GL_TEXTURE_2D, TextureID);
  // BIND POSITION BUFFER
  //glEnableVertexAttribArray(0);
  glBindBuffer(GL_ARRAY_BUFFER, this->PositionBufferID);
  glBufferData(GL_ARRAY_BUFFER,
               sizeof(Mesh_Box_Line::g_position_buffer_data_lines),
               &Mesh_Box_Line::g_position_buffer_data_lines[0],
               GL_STATIC_DRAW   );
  // BIND COLOR BUFFER
  //glEnableVertexAttribArray(1);
  glBindBuffer(GL_ARRAY_BUFFER, UVBufferID);
  glBufferData(GL_ARRAY_BUFFER,
               sizeof(Mesh_Box_Line::g_color_buffer_data_lines),
               &Mesh_Box_Line::g_color_buffer_data_lines[0],
               GL_STATIC_DRAW   );

  // now for the drawing
  //Vertices
  glEnableVertexAttribArray(0);
  glBindBuffer(GL_ARRAY_BUFFER, this->PositionBufferID);
  glVertexAttribPointer(0,
                        3,
                        GL_FLOAT,
                        GL_FALSE,
                        0,
                        (void*) 0   );
  // color
  glEnableVertexAttribArray(1);
  glBindBuffer(GL_ARRAY_BUFFER, this->UVBufferID);
  glVertexAttribPointer(1,
                        3,
                        GL_FLOAT,
                        GL_FALSE,
                        0,
                        (void*) 0   );
  // gl settings
  // gl blend function
  // draw call
  glDrawArrays(GL_LINES, 0, 12*2 );

  // Now, disable bound arrays
  glDisableVertexAttribArray(0);
  glDisableVertexAttribArray(1);
  // disable anything enabled

  return;
}

void
Mesh_Box_Line::cleanup()
{
  // Delete buffers
  glDeleteBuffers(1, &PositionBufferID);
  glDeleteBuffers(1, &UVBufferID);

  // Delete Texture
  glDeleteTextures(1, &TextureID);

  // Delete Shader
  glDeleteProgram(ProgramID);

  return;
}

我觉得奇怪的是我在文件Mesh_Box_Line.cpp中有虚拟函数的实现,我已经检查了拼写和所有内容。

关于消息来源的任何想法/我错过了什么?

1 个答案:

答案 0 :(得分:1)

问题是Mesh_Box_Line.cpp不是Xcode项目文件的构建规则的一部分。如果您收到此错误并且您确定您的代码是正确的,请确保.cpp是您的ide中构建的一部分。