- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 C++ 的新手,我正在尝试开发一个 2D 模拟,其中每个表示的元素都是 Point 的子类。我有几个每个子类的列表(listA,listB ...)。
我正在尝试实现一个找到离当前点最近的点的函数,例如,哪个 B 实例最接近当前 A 实例。将此函数放在 Point 类中对我来说很有意义,因为每个子类都可以使用它,但是由于我的程序使用专门项目的列表( std::list<A>
而不是 std::list<Point>
),我收到以下代码的错误:
class Point
{
Point* findClosest(std::list<Point*>&);
};
class A : public Point
{
void update(std::list<B*>&);
};
class B : public Point
{
};
void A::update(std::list<B*>& blist)
{
B* closest = this->findClosest(blist); //bug
// do something...
}
错误信息:
error: no matching function for call to ‘A::findClosest(std::__cxx11::list<A*>&)’
note: no known conversion for argument 1 from ‘std::__cxx11::list<A*>’ to ‘std::__cxx11::list<Point*, std::allocator<Point*> >&’
如果我使用 Point 列表而不是专用元素列表,它会起作用,但每次我需要使用特定于该类的方法(如 update()
对于 A 类)。
有什么方法可以将我的 A 列表解释为点列表?
感谢帮助!
最佳答案
你不能这样做因为list<B*>
不继承自 list<Point*>
.
在您的情况下,您可能不得不仅使用 list<Point*>
来工作并实例化每个 Point
作为 A
或 B
: 让 Point
的虚函数为你做分离。虽然,一般来说,由于各种原因我不推荐这样的设计(由于使用过多的分配和释放会导致内存碎片问题,因此运行时间会很慢;也会有太多的虚函数调用;不要忘记内存泄漏,以防您忘记释放)
如果你真的想通过list<B*>
对于任意类,那么您将不得不使用模板,但我认为这些对您来说还为时过早。
class Point
{
template<class T>
Point* findClosest(std::list<T*>&)
{
// write implementation here in .h; not in .cpp
}
};
关于c++ - 带有参数列表的父类(super class)方法 <superclass> 与专用列表一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53824429/
我试图让通用链接在我的应用程序中作为 URL 方案的替代品工作,因为从 iOS 9 开始,系统看起来每次从 Safari 调用应用程序时都会显示一个警告对话框。我希望在使用通用链接时替换关联域中注册
对于给定的常量Map val ctt = Map("a" -> 1, "b" -> 2) 如何定义一个String插值器c在哪里 c"a" 交付List(1)? 注意已考虑String Interpo
最近,我强化了我的 Keycloak 部署以使用专用的 Infinispan 集群作为 remote-store为 Keycloak 的各种缓存提供额外的持久层。更改本身进行得相当顺利,尽管在进行此更
我有一个非常明确的问题: //.h file @property (nonatomic, retain)NSMutableString * retainString; @property (nonat
我倾向于厌恶代码中的重复,所以当我遇到唯一不同的是类型的问题时,我倾向于使用泛型。来自 C++ 背景,我发现 vb.net 的版本相当令人沮丧,我知道 C++ 有模板特化,我猜 vb.net 没有所以
问题:如何确认我的“专属服务器”是否正常运行? 背景:我正在努力获得在独立 Linux 系统上运行的“专用 CoreNLP 服务器”。该系统是运行 CentOS 7 的笔记本电脑。选择该操作系统是因为
我一直在痛苦地寻找公平的电话,说 Clouding 把这个和那个给了管理层。到目前为止,无论我在同一个专用/或共享网络托管中找到什么,但云计算如何在软件行业产生巨大影响? 我一直在寻找关于什么是云托管
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 5 年前。 此帖子已于
我没有网络背景。我是数据平台解决方案架构和数据/人工智能工程师。由于分布式数据处理和使用容器进行横向扩展已经进入我的世界,我决定硬着头皮学习 IP 基础知识,以便我可以构建集群。 我一直在做大量的培训
我是 Prototype 的长期用户,总体上非常满意。我最近改用 jQuery 是因为得到了庞大的社区支持,基本上可以说是一致的选择和事实上的行业标准。从那以后我就没那么开心了。是的,我已经阅读了比较
我的应用程序使用各种繁重的库和数据文件,需要在启动时加载/与本地存储同步。由于此操作需要一些时间,因此我正在考虑为此目的创建一个专门的 Activity 。此 Activity 将是应用程序的入口点,
我有一个正在开发的 C++ 程序,它处理许多不同类型的对象,这些对象需要在启动时从文件中检索。总文件大小略小于 1GB。该程序必须至少在 Windows 7 和 Linux 上运行。 我的目标是提供读
我正在为我的页面制作适合移动设备的样式表。有没有一种简单的方法让它向 iPhone/Android 用户显示该样式表?或者我是否必须拉出用户代理并以这种方式解决 - 我该怎么做? 还有 - 有什么工具
我发现各种元素非常困惑。几乎每个元素似乎都有一个与之关联的“部分”,我不确定它们是如何粘合在一起的。 工作簿工作簿部分工作表工作表部分 我也对 DocumentFormat.OpenXml.Packa
我有一个由多个 Zend Framework 应用程序组成的服务器。 我想知道将 Zend Library 上传到服务器并在所有应用程序之间共享而不是按应用程序上传是否是个好主意。 例如,如果多个应用
我们有一个简单的表,需要转换为 XML Declare @Person TABLE ( [BusinessEntityID] [int] NOT NULL, [PersonType] [
当我重新部署容器组时,私有(private) IP 地址发生变化。它通常是三个地址之一。因此,我认识到我的负载均衡器只能拥有所有这三个名称,并且似乎知道哪一个当前实际上处于事件状态,但我更希望能够让负
我目前正在尝试通过我的私有(private) aks 集群部署我的 Helm Chart。但是,我无法执行任何操作,因为它找不到本地目录的路径。 这是我正在运行的命令: az aks command
我已为 Azure 存储帐户设置专用终结点。现在,这一切都为我创建了专用端点和专用链接。此外,我还可以直接从同一事件中的虚拟机访问我的存储帐户(storageaccountA)。 现在,从我的虚拟机进
我们的组织拥有 Azure Synapse 专用池实例。我正在尝试向 Azure Purview 注册 Azure Synapse 专用池并想要扫描 Synapse DB。但是,我每次都会收到以下错误
我是一名优秀的程序员,十分优秀!