The return type is not identical to the covariant with the return type "MediaDevice *" of the overridden virtual function "MediaFactory :: FMediaDevice"

After the factory abstraction:

#include  "MediaDevice.h"

class MediaFactory {
public:
    MediaFactory();
    virtual ~MediaFactory();
    virtual  MediaDevice * FMediaDevice (int type) = 0;
};

and after the factory, which inherits from abstract facotry:

#include "MediaFactory.h"
class JVCMedDevFactory : public MediaFactory {
public:
    MediaDevice* FMediaDevice (int type) {
        switch ((type_e)type) {
            case CDPlayer_e:
            return new JVCCdPlayer() ;
            case DVDPlayer_e:
                return new JVCVcrPlayer() ;
        }
} 
}; 

Media device:

#include <string>
#include <utility>
using namespace std;
class MediaDevice {
 public:
MediaDevice();
    virtual ~MediaDevice();
virtual void Start () = 0 ;
virtual void Stop () = 0 ;
    virtual void Forward () = 0 ;
virtual void Rewind () = 0 ;
virtual pair <string,string> getName () const = 0;
protected:
pair <string,string> DeviceName;
};

Here's how I define JVC players:

#include "MediaDevice.h"
#include <iostream>
using namespace std;   
class JVCCdPlayer : public MediaDevice {
public:
    JVCCdPlayer(){
            DeviceName.first = "JVC";
            DeviceName.second = "CD";
    }
    void Start (){
    cout << "Playing " << this->getName().first << "," << this->getName().second <<     endl;
}
    void Stop (){
    cout << "Stopped " << this->getName().first << "," << this->getName().second     <<endl;
    }
    void Forward (){
    cout << "Rewind " << this->getName().first << "," << this->getName().second <<endl;
}
void     Rewind (){
    cout << "Forward " << this->getName().first << "," <<this->getName().second <<endl;
    }
    pair <string,string> getName () const{
    return DeviceName;
    }
    ~JVCCdPlayer(){}
   };

And I get the following error

The return type is not identical to the covariant with the return type "MediaDevice *" of the overridden virtual function "MediaFactory :: FMediaDevice"

It is importnat that in visula studio I have a red line under FMediaDevice in the declaration MediaDevice * FMediaDevice (int type) {in the class MedDevFactory. And it doesn’t matter what I return. I can return 0 and still get the error.

Why?

+3
source share
3 answers

From the error message, it seems that either JVCCdPlayer, or JVCVcrPlayer(or both) is not derived from MediaDevice. This is true?

MediaDevice. , :

class JVCCdPlayer : public MediaDevice
{
};

class JVCVcrPlayer : public MediaDevice
{
};

- , MediaDevice .


MediaDevice DeviceName, pair<string,string>, , pair. <utility>. , .

, using namespace std . MediaDevice.h :

#ifndef MEDIA_DEVICE_H
#define MEDIA_DEVICE_H

#include <string>
#include <utility>

class MediaDevice 
{
  public:
     MediaDevice();
     virtual ~MediaDevice();
     virtual void Start () = 0 ;
     virtual void Stop () = 0 ;
     virtual void Forward () = 0 ;
     virtual void Rewind () = 0 ;
     virtual std::pair<std::string,std::string> getName () const = 0;
  private:
     std::pair<std::string,std::string> DeviceName;
};

#endif

, std:: using namespace std.

, :

virtual pair <string,string> getName () const = 0;

? ( , , ).

, DeviceName private, protected, JVCVcrPlayer JVCCdPlayer.

+2

MediaDevice, , JVCCdPlayer JVCVcrPlayer.

, JVCMedDevFactory:: FMediaDevice - switch .

0

Visual Studio 2010 ( ).

  • ;
  • MediaDevice::DeviceName ( ) , ;
  • type_e ;
  • Removing the "pure virtual" qualifier =0from member function declarations MediaDeviceand adding the obvious definition for MediaDevice :: getName ().

In real code, the last step is to add overriding definitions of pure virtual functions to the player classes instead, but I was lazy to write it.

I need to say that at no stage did I see that the "Return type is not identical and not covariant with" diagnostics.

Hope this helps.

0
source

All Articles