开发者

Why does my code result in "cannot instantiate abstract class"?

开发者 https://www.devze.com 2022-12-15 20:43 出处:网络
This is the line where the error occurs: this->_tbfCmdHandler.reset(new Bar()); facade_impl.cpp(202): error C2259:

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 & 开发者_JAVA百科]

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.


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.


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.


Try reading this C++ FAQ


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.


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){}.

0

精彩评论

暂无评论...
验证码 换一张
取 消