继承和模板和虚函数(这可能会变得混乱)

Sii*_*Sii 4 c++ virtual inheritance templates function

只是找到模板的方法,所以尝试了一些东西.

让我知道我在这里做错了什么.

我试图重载一个继承的模板虚拟方法.

// 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;
}
Run Code Online (Sandbox Code Playgroud)

`

这给出了错误:

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>&)
Run Code Online (Sandbox Code Playgroud)

如果这不是正确的方法,那么关于模板继承的一些信息会很棒

Tyl*_*nry 7

next类不会自动继承其父类的构造函数.您必须明确定义任何构造函数.这适用于所有派生类,无论是否涉及模板和虚函数.

如果你想定义一个构造函数next,需要两个Ts并将它们转发给相应的mypair构造函数,你可以这样做:

next (T first, T second)
  : mypair<T,A>(first, second)
{
}
Run Code Online (Sandbox Code Playgroud)

同样,即使不涉及模板,这通常也适用.