简体   繁体   中英

Return type is not identical to nor covariant with return type “MediaDevice*” of overridden virtual function “MediaFactory :: FMediaDevice”

I have following abstract factory:

#include  "MediaDevice.h"

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

and following 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 is:

#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;
};

This is 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

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

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

Why?

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

You've to derive both from MediaDevice . Make sure your definitions look like this:

class JVCCdPlayer : public MediaDevice
{
};

class JVCVcrPlayer : public MediaDevice
{
};

Or somewhere in the hierarchy, MediaDevice should be present.


The class MediaDevice has member data DeviceName which is of type pair<string,string> , but you've not included the header in which pair is defined. So include <utility> . Similarly, make sure you've included all the necessary headers.

Also, I wouldn't write using namespace std in a header file. So I would re-write MediaDevice.h as:

#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

That is, I would qualify each name with std:: instead of using namespace std .

By the way, I don't see the definition of the following pure virtual function:

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

Have you defined it in the derived classes? (although the error doesn't say that this is the problem, but still make sure this as well).

Besides, the member data DeviceName is declared as private which needs to be protected because you're accessing it from the derived classes JVCVcrPlayer and JVCCdPlayer .

MediaDevice is probably not a parent of JVCCdPlayer or JVCVcrPlayer.

Also note that the function JVCMedDevFactory::FMediaDevice is not always guaranteed to return a value - you should have a default case in the switch statement or a default return at the bottom of the function.

After the following changes, Visual Studio 2010 compiled your code (though I put it all in a single source file).

  1. Including all required headers;
  2. Making MediaDevice::DeviceName a protected (not private) member, so that it can be accessed by constructors of player classes;
  3. Defining type_e as an enumeration;
  4. Removing "pure virtual" =0 specifier from declarations of MediaDevice member functions, and adding an obvious definition for `MediaDevice::getName().

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

I need to say that at no stage I saw the "Return type is not identical to nor covariant with" diagnostics.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM