0

I have this class in my header render_object.hpp:

class RenderObject {
public:
  struct OGLVertexAttribute {
    GLuint index;
    GLint size;
    GLenum type;
    GLboolean normalized;
    GLsizei stride;
    void const *pointer;
  };
public:
  RenderObject(std::initializer_list<struct OGLVertexAttribute> attributes);
  ~RenderObject();
public:
  void render();
public:
  void updateIndices(std::vector<GLuint> &indices);
  template<typename T>
  void updateVertices(std::vector<T> &vertices);
private:
  GLuint indexBuffer;
  GLuint vertexArray;
  GLuint vertexBuffer;
private:
  unsigned int indexBufferSize;
};

I would like to make the updateVertices function generic so that it can take vectors of any type (including tuples) so I defined a template for it.

template <typename T>
void RenderObject::updateVertices(std::vector<T> &vertices) {
  glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  glBufferData(GL_ARRAY_BUFFER,
  vertices.size() * sizeof(T), &vertices[0], GL_DYNAMIC_DRAW);
}

However, when I compile, I get this linkage error:

/home/terminal/source/application.cpp:23: undefined reference to `void RenderObject::updateVertices<float>(std::vector<float, std::allocator<float> >&)'

Why is that and how can I fix it?

Whiteclaws
  • 902
  • 10
  • 31
  • 6
    Can't be 100% certain without a bit more information, but this has the smell of [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – user4581301 Jun 23 '20 at 00:28

1 Answers1

1

Best answer I can give is usually with templated classes, it is common to keep the implementation with inside the header file. Otherwise, more additional things are are required along with more syntax for an implementation of templated classes in .cpp files to work.

Also note, if you do this, it is not certain that the a certain compiler will be able to handle the templated classes with implementation in .cpp file.

Owen Murphy
  • 153
  • 1
  • 10