【发布时间】:2010-08-31 02:43:59
【问题描述】:
只是在模板中找到了自己的方法,所以尝试了一些东西。
让我知道我在这里做错了什么。
我正在尝试重载继承的模板虚拟方法。
// 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;
}
`
这给出了错误:
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>&)
如果这不是正确的方法,一些关于模板继承的信息会很棒
【问题讨论】:
标签: c++ templates inheritance function virtual