这是错误:
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;
};
以上Bean类是数据表示,它们被两个不同的层使用。一层使用 Bean接口(interface),只是为了访问数据的 setter/getter 。 ResourceBean由数据访问对象 (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++ 中这样做,而应该坚持使用指向基类的指针 vector 。此外,正因为如此,具有该功能 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::vector 类型引用的无效初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14158348/
在我的gem中,我需要yaml并且在我的本地计算机上运行良好。但是在将我的gem推送到rubygems.org之后,当我尝试使用我的gem时,我收到一条错误消息=>"uninitializedconstantPsych::Syck(NameError)"谁能帮我解决这个问题?附言RubyVersion=>ruby1.9.2,GemVersion=>1.6.2,Bundlerversion=>1.0.15 最佳答案 经过几个小时的研究,我发现=>“YAML使用未维护的Syck库,而Psych使用现代的LibYAML”因此,为了解决
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我有用于控制用户任务的Rails5API项目,我有以下错误,但并非总是针对相同的Controller和路由。ActionController::RoutingError:uninitializedconstantApi::V1::ApiController我向您描述了一些我的项目,以更详细地解释错误。应用结构路线scopemodule:'api'donamespace:v1do#=>Loginroutesscopemodule:'login'domatch'login',to:'sessions#login',as:'login',via::postend#=>Teamroutessc
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain