为什么我的代码会导致“无法实例化抽象类”?

发布于 2024-08-17 14:30:16 字数 955 浏览 4 评论 0原文

这是发生错误的行:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冰雪之触 2024-08-24 14:30:16

你已经写下了你的答案。该类是抽象的,这意味着它具有纯虚方法。因此,您首先必须实现这些方法。

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.

心清如水 2024-08-24 14:30:16

您是从抽象类继承的,因此您需要在类 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.

勿忘初心 2024-08-24 14:30:16

了解多态性以及什么是抽象类。

抽象意味着它尚未完全定义,因此您无法实例化它,因为您还没有弄清楚所有部分。

这就像尝试在没有发动机的情况下启动汽车一样。

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.

初见终念 2024-08-24 14:30:16

尝试阅读此 C++ 常见问题解答

Try reading this C++ FAQ

舟遥客 2024-08-24 14:30:16

这就是问题解决的:

我删除了 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.

对岸观火 2024-08-24 14:30:16

错误显示 void Subscriber::update(T)' : is abstract with T=char &,因此您需要定义 void update( char&),而不仅仅是 void update( const RecoveryState &dummy){}

The error says void Subscriber<T>::update(T)' : is abstract with T=char &, so you need to define void update(char&), not just void update( const RecoveryState &dummy){}.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文