I have the following class structure:

class MyBase
{
   public:
      virtual ExportData exportData() = 0;
      virtual bool exportData(QString filepath)
      {
         ExportData data = exportData();
         data.save(filepath);
      }
};

class MyClass : public MyBase
{
   public:
      virtual ExportData exportData(){//some implementation}
};

class MySubClass : public MyClass
{
   public:
      virtual ExportData exportData(){//some implementation}
};

Then I export the data as follows:

MySubClass *sub = new MySubClass();
sub->exportData("/home/me/export.xml");

When trying to compile with g++, I get the following error:

error: no matching function for call to ‘MySubClass::exportData(QString)’
note: candidate is: virtual ExportData MySubClass::exportData()
note:   candidate expects 0 arguments, 1 provided

I don't see something I did wrong, why is this happening?

有帮助吗?

解决方案

I suspect that your first virtual function hides your second function, you should do this in your subclass:

using MyBase::exportData;

Making this function explicitly visible to your subclass.

Live Example

其他提示

By declaring virtual ExportData exportData() in MySubClass, you're hiding virtual bool exportData(QString filepath). You need to bring it to scope with a using declaration:

class MySubClass : public MyClass
{
   public:
      using MyBase::exportData;
      virtual ExportData exportData(){//some implementation}
};
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top