为什么我不能将类指针传递给访问者类?它说没有匹配的功能
Why can I not pass my class pointer to my visitor class? It says there are no matching functions
本质上,这个访问者类一直在工作,直到我试图向它添加另一个类。
这是我要提到的访问者类:
访问者.h
#ifndef Visitor_h__
#define Visitor_h__
#include <stdint.h>
#include <string>
#include <vector>
namespace Ph2_System
{
class SystemController;
}
namespace GUI
{
class SystemControllerWorker;
}
class HwDescriptionVisitor
{
public:
//SystemController
virtual void visit( Ph2_System::SystemController& pSystemController ) {} //This one works
//SystemControllerWorker
virtual void visit( GUI::SystemControllerWorker& pSystemControllerWorker ) {}
}
这是一个试图访问它的类:
SystemControllerWorker.h
#include "../HWInterface/Visitor.h"
#include "Model/settings.h"
namespace GUI
{
class SystemControllerWorker
{
public:
SystemControllerWorker(Settings &config);
~SystemControllerWorker();
void accept( HwDescriptionVisitor& pVisitor ) const {
pVisitor.visit( *this ); // this is where I get my error message
for ( auto& cShelve : fShelveVector )
cShelve->accept( pVisitor );
}
private:
Settings& m_Settings; //takes in the above &config in the contructor
SystemController
和SystemControllerWorker
之间的唯一区别是第一个不接受任何参数,而这一个接受。知道我为什么会收到这个错误消息吗:
error: no matching function for call to "HwDescriptionVisitor::visit(const GUI::SystemControllerWorker&)"
pVisitor.visit( *this );
或者这可能意味着什么?
有时读取整个错误消息是件好事,编译器真的愿意帮助您理解它为什么会失败:
main.cpp: In member function 'void SystemControllerWorker::accept(HwDescriptionVisitor&) const':
main.cpp:15:29: error: no matching function for call to 'HwDescriptionVisitor::visit(const SystemControllerWorker&)'
pVisitor.visit(*this);
^
main.cpp:15:29: note: candidate is:
main.cpp:8:10: note: void HwDescriptionVisitor::visit(SystemControllerWorker&)
void visit(SystemControllerWorker& pSystemControllerWorker) {}
^
main.cpp:8:10: note: no known conversion for argument 1 from 'const SystemControllerWorker' to 'SystemControllerWorker&'
最后一个note
是最重要的;它说:"参数1从‘const-SystemControllerWorker’到"SystemControllerWorker&"没有已知的转换
您可能想知道为什么this
是const SystemControllerWorker
类型,而不仅仅是SystemControllerWorker
。这是因为调用pVisitor.visit(*this)
的方法本身被标记为const
:
void accept( HwDescriptionVisitor& pVisitor ) const {
// right here ~~~~^
这使得this
成为指向const SystemControllerWorker
的指针,取消引用也会导致const
类型。
根据应该允许访问的操作,您可以从accept
成员函数中删除const
限定符
或
向visit
成员函数的SystemController&
参数添加const
限定符,如下所示:
virtual void visit( const Ph2_System::SystemController& pSystemController ) {}
// here ~~~~^
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 访问者访问变体并返回不同类型时出错
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 对可变参数使用声明.如何选择正确的功能
- 获取具有AST访问者clang的功能声明类型
- 为什么我不能将类指针传递给访问者类?它说没有匹配的功能