I just recently was learning templates in C++. But I got 3 errors even when I do everything exactly like in my course.
This is the main.cpp:
#include <iostream>
#include "szablony.h"
using namespace std;
int main()
{
cout << nmax<int>(55,402) << endl;
Klasa<double> a1;
a1.ustaw(25.54);
Klasa<double> a2;
a2.ustaw(44.55);
cout << a1.podaj() << " :max: " << a2.podaj() << " = " <<
nmax<Klasa>(a1.podaj(),a2.podaj()) << endl;
}
And this is "szablony.h":
#include <iostream>
using namespace std;
template <typename T> class Klasa
{
T wartosc;
public:
template <typename U> T podaj()
{
return (this -> wartosc);
}
template <typename U> void ustaw(U war)
{
wartosc=war;
}
};
template <typename T, typename T1, typename T2> T nmax(T1 n1, T2 n2)
{
return (n1 > n2 ? n1 : n2);
}
template <> Klasa nmax<Klasa>(Klasa n1, Klasa n2)
{
return (n1.podaj() > n2.podaj() ? n1 : n2);
}
So these are the errors:
"szablony.h":|line 27|error: invalid use of template-name 'Klasa' without an argument list|
main.cpp|line 16|error: no matching function for call to 'Klasa::podaj()'|
main.cpp|line 17|error: no matching function for call to 'Klasa::podaj()'|
This course is from 2004 btw, that's probably one reason, but even when I look on the internet, everything seems ok...
Thank you in advance :)
The main problem is that Klasa
is a template class, but you use it in the specialization of nmax
as a regular class. In particular, Klasa
does not represent a type, but eg Klasa<int>
does.
So either make your function return a template template, or use Klasa<type>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.