vector类型的引用初始化无效

invalid initialization of reference of type std::vector

本文关键字:初始化 无效 引用 类型 vector      更新时间:2023-10-16

错误:

DummyService.hpp:35:错误:无效的协变返回类型为"虚拟"std::向量& lt;ResourceBean *,std::分配器& lt;ResourceBean *>>,DummyService:列表(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;
};

以上Bean类是数据表示,它们被两个不同的层使用。一层使用Bean接口,只是为了访问数据的getter。ResourceBean由DAO (data access object,数据访问对象)类访问,DAO类从数据库(例如)获取数据,并填充ResourceBean

DAO的一个职责是列出给定某个路径的资源:

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

我认为这个问题与std::vector<ResourceBean*>中编译器不理解Bean实际上是ResourceBean的基类有关。

有什么建议吗?我读过一些类似的主题,但有些解决方案在我的情况下不起作用。如果我遗漏了什么,请指出来。

std::vector<Bean*>std::vector<ResourceBean*>是两个完全不同的类型,不能相互转换。在c++中根本无法做到这一点,而应该坚持使用指向基类的指针向量。同样,因为这个原因,拥有virtual函数并没有做你认为它做的事情——不同的返回类型使它成为不同的方法签名,所以你不是重载一个方法,而是引入一个新的虚方法。此外,没有必要为抽象类(Service)创建受保护的构造函数,因为无论如何都不能创建抽象类的实例。我会这样写:

#include <string>
#include <vector>
#include <iostream>
class Bean {
  public:
    typedef std::string Path;
    Bean() {}
    virtual ~Bean() {}
    virtual void say() const
    {
        std::cout << "I am a bean!n";
    }
};
class ResourceBean : public Bean {
  public:
    ResourceBean() {}
    virtual ~ResourceBean() {}
    virtual void say() const
    {
        std::cout << "I am a resource bean!n";
    }
};
class Service {
public:
    Service() {}
    virtual ~Service() {}
    virtual std::vector<Bean*>& list(const Bean::Path &path) = 0;
};
class DummyService: public Service {
  public:
    DummyService()
    {
        for (int i = 0; i < 5; ++i)
            beans_.push_back(new ResourceBean());
    }
    virtual ~DummyService() {}
    virtual std::vector<Bean *>& list(const Bean::Path &path)
    {
        return beans_;
    }
  private:
    std::vector<Bean*> beans_;
};
int main()
{
    DummyService s;
    std::vector<Bean *>& l = s.list("some path");
    for (std::vector<Bean *>::iterator it = l.begin(), eit = l.end();
         it != eit; ++it)
    {
        Bean *bean = *it;
        bean->say();
    }
}