I have the following code which I conceived solely in order to practice function templates.
#include <iostream>
template <typename T>
T fun( const T &t ) { return t; }
struct A {
int dataf;
A( int a ) : dataf(a) { std::cout << "birth\n"; }
friend A fun( const A & );
};
int main(){
A a( 5 );
fun( a );
return 0;
}
Though I get the following error:
code.cc:(.text+0x32): undefined reference to `fun(A const&)'
collect2: ld returned 1 exit status
I understand well class templates, but I am still confused about function templates.
Change the friend declaration to:
template <class T> friend T fun( const T & );
or to:
friend A fun<A>( const A & );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With