문제

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