C++ Template, Linker undefined reference despite instantiation? -
C++ Template, Linker undefined reference despite instantiation? -
this question has reply here:
why can templates implemented in header file? 9 answersi tried create template class implementation of 3d vector. relevant snippet of 2 files (vec3d.h , vec3d.cpp) here on pastebin.
the main.cpp follows:
#include "vec3d.h" #include <iostream> using namespace std; int main() { vec3d<double> a(1,2,3), b(2,4,5), c; c = 2.3*b; cout<<c._x; homecoming 0; }
on compiling g++ main.cpp vec3d.cpp vec3d.h
next error occurs:
in function `main': main.cpp:(.text+0x124): undefined reference `vec3d<double> const operator*<double>(double, vec3d<double> const&)' collect2: error: ld returned 1 exit status
i have set definition , declarations of functions separately, have instantiated template double
.
funnily enough, c = b*2.3
not throw error. reasons? getting same error on several other functions (like <<, ==, / declared in similar fashion)
what doing wrong?
[[edit]]
i instantiated class, should be, didn't instantiated template functions nowadays outside of class. apparently, have instantiate too. didn't know that.
more: refer this question. every c++ unit defined using template has instantiated if definitions in separate file file declarations, not classes
the operators required not instantiated have (despite class' instantiation), not members of class. hence declared definition not found.
you should implement entire template class , associated operators in header.
c++ templates linker undefined-reference
Comments
Post a Comment