没有匹配的函数来调用

No matching function for to call

本文关键字:函数 调用      更新时间:2023-10-16

我是继承的新手。

#include<iostream>
using namespace std;
class base
{ public:
    void show()
     {
       //do something here
         }  
    };
class derive:public base
{ public:
 void show(int n,int m)
    { 
      //do something
        }};

int main()
{
derive D;
  D.show();
  D.show(4,5);
    }

因此,编译器给出的错误是:调用 'derive::show(( 时没有匹配函数

编译器处理时

D.show();

它首先检查名称show是否存在于derive中。如果是,则不会在基类中查找名称。之后,它会尝试为其找到最佳匹配项。在这种情况下,没有匹配,因为 derive 中唯一名为 show 的函数需要两个类型 int 的参数。因此

D.show();

无效。

如果你想让base::showderive中作为重载可用,你必须让编译器知道。

class derive : public base
{
   public:
      // Let the functions named show from base be available
      // as function overloads.
      using base::show;
      void show(int n,int m)
      {
         cout<<"Derive Version.."<<n<<","<<m<<endl;
      }
};

现在,您可以使用:

int main()
{
   derive D;
   D.show();
   D.show(4,5);
}

编译器是正确的。在derive类中没有函数show()。我猜你想要的是访问base类函数。为此,必须指定它是基类,而不是派生类。为此,您需要做的就是将范围限定为基类,执行以下操作:

derive D;
D.base::show();       //Uses the bases function
D.derive::show(4,5);  // Uses the derived function
D.show(4,5);          // Uses the derived function