vector类型的引用初始化无效
invalid initialization of reference of type std::vector
错误:
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();
}
}
相关文章:
- C++-模板嵌套类的引用初始化无效
- 对全局变量的非常量引用的初始化无效
- 对类型为 std::_Bit_rerefence& 的非常量引用的初始化无效
- 模板流运算符重载错误:引用初始化无效,与basic_istream和basic_ifstream之间的差异有关
- 'inherit class'中类型 'template class' 引用的初始化无效
- 类型为"短整型 (&)"的引用初始化无效
- 类型为'std::string&的非常量引用的初始化无效,并且从类型为'std::basic_string<char>的右值
- 类型为 'cv::Point2f&' 的非常量引用的初始化无效
- 使用 boost::any_range 时非常量引用错误的初始化无效
- 指针初始化无效?如果不是,那是什么
- 引用的初始化无效:指针和值之间存在差异
- 使用具有多态性的引用时初始化无效
- 错误:非整型 'const char[]' 的静态数据成员的类内初始化无效
- 类型引用的初始化无效
- Visual C++ 2015 中右值的非常量引用初始化无效
- 类型为 std::String&- 的非常量引用的初始化无效 - 如何删除
- Cpp.中的私有静态数据成员只能在其定义时初始化,而类内初始化无效
- 错误:类型初始化无效
- 类型'int&'的引用初始化无效,传递参数 1 时出错
- 类型为'std::string&的非常量引用的初始化无效