Frage

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?

War es hilfreich?

Lösung

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

Andere Tipps

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}
};
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top