This is the error:
DummyService.hpp:35: error: invalid covariant return type for 'virtual std::vector < ResourceBean*, std::allocator < ResourceBean*> >& DummyService::list(const std::string&)'
class Bean {
public:
typedef std::string Path;
virtual ~Bean() {};
virtual const Path& getPath() = 0;
virtual const std::string& getName() = 0;
protected:
Bean();
};
class ResourceBean: public Bean {
public:
ResourceBean(const Path& path, const std::string& contents) :
_path(path), _contents(contents) {
}
virtual ~ResourceBean() { }
virtual const Path& getPath();
virtual void setPath(const Path& path);
virtual const std::string& getName();
virtual void setName(const std::string& name);
private:
Path _path;
std::string _name;
};
The above Bean
classes are data representations, and they are used by two different layers. One layer uses the Bean
interface, just to access the getters for the data. The ResourceBean
is accessed by the data access object (DAO) classes, which take the data from a database (for example), and fill in the ResourceBean
.
One responsibility of the DAO is to list the resources given a certain path:
class Service {
protected:
/*
* Service object must not be instantiated (derived classes must be instantiated instead). Service is an interface for the generic Service.
*/
Service();
public:
virtual std::vector<Bean*>& list(const Bean::Path& path) = 0;
virtual ~Service();
};
class DummyService: public Service {
public:
DummyService();
~DummyService();
virtual std::vector<ResourceBean*>& list(const ResourceBean::Path& path);
};
I think the problem is related with the fact that in std::vector<ResourceBean*>
the compiler does not understand that Bean
is actually the base class of ResourceBean
.
Any suggestions? I've read to some similar topics but some of the solutions did not work in my case. Please point out if I have missed something. Thank you in advance.