c++ - G++ Template instantiation results in "Undefined reference to" error -


i've been having issues lately custom written general purpose vector code, relies on templates functionality. reluctant include implementation in header file (as common templates), add compilation time. have instead manually instantiated desired class in .cpp file. however, still results in undefined reference error. have reduced code following snippet, still generates error:

matrixd.cpp #include "matrixd.h"  namespace math {     template class _vec2<float>;     template<class t> _vec2<t>::_vec2() {} } 

matrixd.h

#pragma once namespace math {      template <class t>     class _vec2     {     public:         t x, y;         _vec2<t>();         void reset();     };      typedef _vec2<float> vec2; } 

test.cpp

#include "matrixd.h"  int main() {     math::_vec2<float> v; } 

error message:

in function main': source.cpp:(.text+0x10): undefined reference math::_vec2::_vec2()' collect2: error: ld returned 1 exit status

any appreciated! :)

a explicit instantiation definition (template class _vec2<float>; in code) instantiates member functions have been defined @ point of explicit instantiation. _vec2<t>::_vec2() defined after explicit instantiation definition, , not explicitly instantiated.

the fix swap 2 lines:

namespace math {     template<class t> _vec2<t>::_vec2() {}     template class _vec2<float>; } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -