将抽象类对象从映射存储到抽象类引用
我正在尝试实现以下方法来获取与其名称相对应的总线,该名称作为字符串传递给该方法。这里 AbstractBus 是一个包含总线功能的抽象类。 localBusses 是一个 std::map
void getLocalBusByName(string& name, AbstractBus& bus)
{
bus = localBusses.find(name)->second;
}
但是,我收到错误“没有运算符”= 与这些操作数匹配” 谁能解释一下这里出了什么问题以及我该如何纠正它?
I am trying to implement the following method to get the bus corresponding to its name which is passed as a string to the method. Here AbstractBus is an abstract class which contains functionality of a bus. localBusses is a std::map
void getLocalBusByName(string& name, AbstractBus& bus)
{
bus = localBusses.find(name)->second;
}
However, I am getting an error "no operator "=" matches these operands"
Can anyone explain what is wrong here and how can I correct it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码提出了几个问题
编辑:我相信你要么有:
要么 AbstractBus 并不是真正抽象的(不包含任何纯方法)(尽管它的名字)
在任何一种情况下我都假设你有从 AbstractBus 派生的类。 getLocalBusByName 必须应对找不到总线的情况并避免无意的切片。
因此,您应该在以下行中编写 getLocalBusByName 方法:
其中 localBusses 被假定为类型:
std::map
(如果 AbstractBus 是抽象的,这是唯一可行的方法)The code presents several problems
EDIT: I believe you either have:
or AbstractBus is not really abstract (does not contain any pure methods) (despite its name)
In either case I assume however you have classes derived from AbstractBus. And getLocalBusByName has to provide for situations where no bus is found and avoid inadvertent slicing.
So you should write getLocalBusByName method in the lines of:
where localBusses is assumed to be of type:
std::map<string, AbstractBus*>
(the only viable way if AbstractBus is abstract)