【问题标题】:Inheritance and templates and virtual functions ( this can get messy)继承、模板和虚函数(这可能会变得混乱)
【发布时间】: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


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 首先感谢您的回复。所以如果我要为 next 实例化一个对象(因为它不是模板),我该怎么做?我尝试了以下两种方法 next newobj(100.25,77.75) // 我知道这是错误的,因为模板无法确定它是什么数据类型 next newobj(100.25,77.75); //我得到一个错误,下一个不是模板..正确所以我应该怎么做。
    • next&lt;double, float&gt; newobj(100.25, 77.75) 是正确的做法。一定有其他问题。您是否忘记了 next 类中的 public:?我不认为您的意思是让 next::getmax 成为私有方法。缺少可能会导致其他问题。
    • 我确实将 getmax 更改为 public。但是使用第二种方式编译器给出的错误下一个不是模板,我认为因为下一个不是模板 'next newobj( 100.25, 77.75)' 会错吗? (让人想到下一步是一个模板)
    • 您必须在编译与您发布的代码不同的代码。如果我从您的问题中获取代码,从我的答案中添加构造函数,并添加 public: 标签,它会为我编译。
    • template &lt;class T, class A&gt; class next (T first, T second) : mypair&lt;T,A&gt;(first, second) { a=first; b=second; } template &lt;class T, class A&gt; class next: mypair&lt;T,A&gt; { public: A getmax () { cout&lt;&lt;"WOO HOO"; } }; int main () { mypair &lt;double,float&gt; myobject(100.25, 75.77); next&lt;double,float&gt; newobject(100.25, 75.77); cout &lt;&lt; newobject.getmax(); return 0; } 这是我所做的更改。仍然报错
    【解决方案2】:

    如果有人感兴趣,可以提供完整的解决方案(感谢 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;
    } 
    

    【讨论】:

      猜你喜欢
      • 2010-10-11
      • 1970-01-01
      • 2012-09-20
      • 2014-11-11
      • 2014-05-15
      • 2018-06-18
      • 2021-06-22
      • 2012-05-12
      • 2015-08-23
      相关资源
      最近更新 更多