Help With Template Class Declarations
Apr 24, 2014 at 6:16am UTC
I'm getting this error "Error 1 error C2955: 'ONE' : use of class template requires template argument list " and i think it's because of something I'm missing in the friend function but I am not sure.
What does the error mean?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <iostream>
#include <string>
using namespace std;
template <class T>
class ONE
{
private : T x,y;
public : ONE();
ONE(T a, T b);
void MaxMin (T& mx, T& mn);
friend void Display(ONE P);
~ONE();
};
template <class T>
ONE<T>::ONE()
{x=0, y=0;}
template <class T>
ONE<T>:: ONE(T a, T b)
{x=a, y=b;}
template <class T>
void ONE<T>::MaxMin(T& mx, T&mn)
{(x>y)?(mx=x, mn=y):(mx=y,mn=x);}
template <class T>
void Display(ONE P)
{cout<<"x==" <<P.x<<"\t" <<"y=" <<P.y<<endl;}
template <class T>
ONE<T>::~ONE(){}
int main()
{
ONE<int > r;
ONE<string> s("Tom" , "Adam" );
Display(r);
Display(s);
string max, min;
s.MaxMin(max, min);
cout<<"Maximum=" <<max<<"\t" <<"Minimum=" <<min<<endl;
system("pause" );
return 0;
}
Apr 24, 2014 at 6:31am UTC
1. you should use
template <typename T>
instead of
template <class T>
in this case
2.
1 2
void Display(ONE P)
{cout<<"x==" <<P.x<<"\t" <<"y=" <<P.y<<endl;}
should be
void Display(ONE<T> P)
. Also this function is trying to access private members of
class ONE
. Make getters or make display class method.
Apr 24, 2014 at 6:36am UTC
Ok, i changed it to template<typename T> but I'm still getting errors.
I haven't learn about getters or make display class method. Mind sharing a link where i can see how to do that?
Apr 24, 2014 at 12:49pm UTC
changes are in bold font
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <iostream>
#include <string>
using namespace std;
template <typename T>
class ONE
{
private : T x,y;
public : ONE();
ONE(T a, T b);
void MaxMin (T& mx, T& mn);
friend void Display(ONE P);
~ONE();
//you need these to access private members liek x and y
T getX(){return x;}
T getY(){return y;}
};
template <typename T>
ONE<T>::ONE()
{x=0, y=0;}
template <typename T>
ONE<T>:: ONE(T a, T b)
{x=a, y=b;}
template <typename T>
void ONE<T>::MaxMin(T& mx, T&mn)
{(x>y)?(mx=x, mn=y):(mx=y,mn=x);}
template <typename T>
void Display(ONE<T> P)
{cout<<"x==" <<P.getX() <<"\t" <<"y=" <<P.getY() <<endl;}
template <typename T>
ONE<T>::~ONE(){}
int main()
{
ONE<int > r;
ONE<string> s("Tom" , "Adam" );
Display<int > (r);
Display<string> (s);
string max, min;
s.MaxMin(max, min);
cout<<"Maximum=" <<max<<"\t" <<"Minimum=" <<min<<endl;
system("pause" );
return 0;
}
Apr 25, 2014 at 6:42am UTC
Thank you for that. I will look more into getters but so far I seem to understand what you did.
Topic archived. No new replies allowed.