views:

89

answers:

2

Just finding my way around templates so was trying out a few stuff.

Let me know what I am doing wrong here.

I am trying to overload a inherited templates virtual method.

// class templates
#include <iostream>
using namespace std;

template <class T, class A>
class mypair {
    T a, b;
  public:
    mypair (T first, T second)
      {a=first; b=second;}
    virtual A getmax (); 
};

template <class T, class A>
A mypair< T, A>::getmax ()
{
  A retval;
  retval = a>b? a : b;
  return retval;
}



template <class T, class A>
class next : public mypair <T, A> {
        A getmax ()
        {   
        cout <<" WHOO HOO";
        }   
};


int main () {
  mypair <double,float> myobject(100.25, 75.77);
  next<double,float>  newobject(100.25, 75.77);
  cout << myobject.getmax();
  return 0;
}

`

This gives the error :

function.cpp: In function ‘int main()’:
function.cpp:35: error: no matching function for call to ‘next<double, float>::next(double, double)’
function.cpp:25: note: candidates are: next<double, float>::next()
function.cpp:25: note:                 next<double, float>::next(const next<double, float>&)

If this isnt the right way to proceed, some info on template inheritance would be great

+6  A: 

The next class does not automatically inherit the constructors from its parent class. You have to define any constructors explicitly. This applies to all derived classes, whether template and virtual functions are involved or not.

If you want to define a constructor from next that takes two Ts and forwards them to the corresponding mypair constructor, you would do it like this:

next (T first, T second)
  : mypair<T,A>(first, second)
{
}

Again, this is generally applicable even without templates involved.

Tyler McHenry
Firstly thanks for the reply.so if I were to instantiate an object for next (as it is not a template) How will I do it? I tried the following two waysnext newobj(100.25,77.75) // i know this is wrong as the template wouldnt be able to figure out what data type it is next<double, float> newobj(100.25,77.75);//I get an error that next is not a template..rightly soHow am I supposed to do it then.
Sii
`next<double, float> newobj(100.25, 77.75)` is the right way to do it. There must be something else wrong. Did you forget the `public:` in the `next` class? I don't think that you meant for `next::getmax` to be a private method. Missing that might be causing other problems.
Tyler McHenry
I did change the the getmax to public.But with the second way the complier give the error next is not a template and I think since the next isnt a template 'next<double, float> newobj(100.25, 77.75)' would be wrong? ( makes one think next is a template)
Sii
You must be compiling code other than what you posted. If I take the code from your question, add the constructor from my answer, and add the `public:` label, it compiles for me.
Tyler McHenry
`template <class T, class A>class next (T first, T second) : mypair<T,A>(first, second){ a=first; b=second;}template <class T, class A>class next: mypair<T,A>{public: A getmax () { cout<<"WOO HOO"; }};int main () { mypair <double,float> myobject(100.25, 75.77); next<double,float> newobject(100.25, 75.77); cout << newobject.getmax(); return 0;}`This is the change that I did.Still gives error
Sii
Hey thanks, I was thinking overly complicated.Solved it.
Sii
+1  A: 

Full Solution if anyone is interested ( thanks to Tyler)

// class templates
#include <iostream>
using namespace std;

template <class T, class A>
class mypair {
         T a, b;
  public:
    mypair (T first, T second)
      {a=first; b=second;}
    virtual A getmax (); 
};

template <class T, class A>
A mypair< T, A>::getmax ()
{
  A retval;
  retval = a>b? a : b;
  return retval;
}







template <class T, class A>
class next: mypair<T,A>
{
public:

        next (T first, T second) : mypair<T,A>(first, second)
        {   
        }   

        A getmax ()
        {   
        cout<<"WOO HOO";
        }   
};


int main () {
  mypair <double,float> myobject(100.25, 75.77);
  next<double,float>  newobject(100.25, 75.77);
  cout << newobject.getmax();
  return 0;
} 
Sii
+1: for concluding it so nicely
Chubsdad