Compiling: C:\Devel\Untitled1.cpp
C:\Devel\Untitled1.cpp:12: warning: friend declaration `Type Something(Type)' declares a non-template function
C:\Devel\Untitled1.cpp:12: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
Linking console executable: C:\Devel\Untitled1.exe
Process terminated with status 0 (0 minutes, 5 seconds)
0 errors, 2 warnings
It is compiling fine (with a warning though). What do you mean it's not working?
Have You tried something like that ? (It compiles without warnings on GCC)
#include <iostream>
template <class Type>
Type Something(Type k)
{
return k;
}
template <class Type>
class HateAnsi
{
public:
HateAnsi()
{}
friend Type Something<Type>(Type k);
};
int main()
{
std::cout << "Hello world!" << std::endl;
return 0;
}
#include <iostream>
template<long B,unsigned long E>
struct pow_helper
{
static const double value;
};
template<long B,unsigned long E>
const double pow_helper<B,E>::value=B*pow_helper<B,E-1>::value;
template<long B>
struct pow_helper<B,0>
{
static const double value;
};
template<long B>
const double pow_helper<B,0>::value=1;
template<long B,long E>
struct power
{
static const double value;
};
template<long B,long E>
const double power<B,E>::value= E<0 ? 1.0/pow_helper<B,E<0 ? -E : E>::value :
pow_helper<B,E<0 ? -E : E>::value;
int main()
{
std::cout << power<10,-3>::value << std::endl;
}
or this sample compiles without warnings too and does something meaningful also ;-)
You should put " = Type()" only in the definition and remove it in the declaration. In other words, it should be specified once and only in the definition.
Try this:
template <class Type>
class Something
{
public:
Something()
{}
void Remove(const Type & data = Type());
};
template <class Type>
void Something<Type>::Remove(const Type & data)
{}
int main()
{
return 0;
}