为什么我的代码会导致“无法实例化抽象类”?
这是发生错误的行:
this->_tbfCmdHandler.reset(new Bar());
facade_impl.cpp(202):错误 C2259: 'FOO::Bar' : 不能 实例化抽象类
由于以下成员:
'void Subscriber::update(T)' : 是 摘要与
T=字符&观察者.h(66) :参见声明 '订阅者::更新'
与
T=字符& 'void Subscriber::update(T)' : 是 摘要与
T=const char &观察者.h(66) :参见声明 '订阅者::更新'
与
T=const char & ]
的声明,
namespace FOO
{
class Facade::Implementation
:public Subscriber<const char& >
{
这是 Facade:: Implementationfacade.cpp
FOO::Facade::Facade () : impl (new Implementation)
{
Singleton<SPM::Facade>::instance ();
}
The update functions:
void update( const char *aMsg)
{
printf("foo");
};
我希望这有助于找出在哪里可以找到错误。
This is the line where the error occurs:
this->_tbfCmdHandler.reset(new Bar());
facade_impl.cpp(202): error C2259:
'FOO::Bar' : cannot
instantiate abstract class
due to following members:
'void Subscriber::update(T)' : is
abstract with
T=char &observer.h(66)
: see declaration of
'Subscriber::update'
with
T=char &
'void Subscriber::update(T)' : is
abstract with
T=const char &observer.h(66)
: see declaration of
'Subscriber::update'
with
T=const char & ]
This is the declaration for Facade::Implementation
namespace FOO
{
class Facade::Implementation
:public Subscriber<const char& >
{
facade.cpp
FOO::Facade::Facade () : impl (new Implementation)
{
Singleton<SPM::Facade>::instance ();
}
The update functions:
void update( const char *aMsg)
{
printf("foo");
};
I hope this helps to figure out where I can find the error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你已经写下了你的答案。该类是抽象的,这意味着它具有纯虚方法。因此,您首先必须实现这些方法。
You have already written your answer. The class is abstract, which means it has pure virtual methods. So, you first have to implement these methods.
您是从抽象类继承的,因此您需要在类 Facade::Implementation 中实现 void update( const char& ) 函数。
您确实定义了更新函数,但它与订阅者没有任何关系。您必须将其放入您的实现中。
You are inheriting from an abstract class, so you need to implement the void update( const char& ) function inside class Facade::Implementation.
You did define an update function, but it is not related in any way to Subscriber. You have to put it inside your implementation.
了解多态性以及什么是抽象类。
抽象意味着它尚未完全定义,因此您无法实例化它,因为您还没有弄清楚所有部分。
这就像尝试在没有发动机的情况下启动汽车一样。
Read up about polymorphism and what an abstract class is.
Abstract means that it is not completely defined yet and therefore, you cannot instanciate that because you don't have all the parts figured out.
It's like trying to start a car without the enginge.
尝试阅读此 C++ 常见问题解答
Try reading this C++ FAQ
这就是问题解决的:
我删除了 void update( const char &aMsg),并看到还有另一个
“void update( char *aMsg)”
为什么编译器没有报告这个函数真是一个奇迹......
删除非常量和指针(而不是引用)函数最终解决了问题。
This is what the problem solves:
I removed the void update( const char &aMsg), and saw that there was another one
"void update( char *aMsg)"
Why this function wasn't reported by the compiler is a miracle...
removing the non-const and the Pointer(instead of Reference)-function fixes the problem finally.
错误显示
void Subscriber::update(T)' : is abstract
withT=char &
,因此您需要定义void update( char&)
,而不仅仅是void update( const RecoveryState &dummy){}
。The error says
void Subscriber<T>::update(T)' : is abstract
withT=char &
, so you need to definevoid update(char&)
, not justvoid update( const RecoveryState &dummy){}
.