Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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}
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top