Hallo allemaal.
Ja, weer zo'n zeer simpele vraag van mij
Neem dit stukje code :
#include <iostream>
template <typename T>
class calculate
{
public:
T add ( T a, T b );
};
template <typename T> T calculate <T>::add ( T a, T b )
{
return a + b;
}
template <>
class calculate <float> {
public:
float add ( float a, float b )
{
std::cout << "This is the specialized class." << std::endl;
return a + b;
}
};
int main ()
{
calculate <int> a;
calculate <float> b;
std::cout << a.add ( 10, 4 ) << std::endl;
std::cout << b.add ( 10.4, 18.6 ) << std::endl;
return 0;
}
Dit compileert zonder fouten en wordt perfect uitgevoerd. Maar stel dat ik de functie add van de gespecialiseerde class calculate buiten de functie wil declareren, zoals ik dit doe in deze code :
#include <iostream>
template <typename T>
class calculate
{
public:
T add ( T a, T b );
};
template <typename T> T calculate <T>::add ( T a, T b )
{
return a + b;
}
template <>
class calculate <float> {
public:
float add ( float a, float b );
};
template <> float calculate <float>::add ( float a, float b )
{
std::cout << "This is the specialized class." << std::endl;
return a + b;
}
int main ()
{
calculate <int> a;
calculate <float> b;
std::cout << a.add ( 10, 4 ) << std::endl;
std::cout << b.add ( 10.4, 18.6 ) << std::endl;
return 0;
}
dan compileert het programma niet. Ik zou echter niet weten wat hier fout aan is ?
PS : Ja, ik weet het, ik kan ook gewoon de functie binnen de class blijven definiëren, en in dit geval heeft specialisatie totaal geen nut, maar toch
Alvast bedankt !