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?
source
share