C++ does not instantiate templates with, say T = Hoge&
.
A minimal example:
hoge.h
:#include<cstdio> class Hoge { public: Hoge() : hoge(0) { } ~Hoge() { } int hoge; void print() { printf("%d\n", hoge); } }; template<typename T> void f(T a);
hoge.cpp
:#include "hoge.h" template<typename T> void f(T a) { a.print(); } template void f<Hoge &>(Hoge &a);
main.cpp
:#include "hoge.h" int main(void) { Hoge h; f(h); return 0; }
I compiled these with: g++ -std=c++11 main.cpp hoge.cpp
. But it gives a linker error:
Undefined symbols for architecture x86_64:
"void f<Hoge>(Hoge)", referenced from:
_main in aa-e35088.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Next, I changed f(h)
in main.cpp
to f<Hoge &>
, and the error disappeared.
Why is f<Hoge &>(Hoge &)
not called in the first case?
For this case, I can avoid errors by typing f<Hoge &>
every time. But, when it comes to overloaded operators, it cannot be done.
Please tell me how to solve this error.