c++ - Implementing this Abstract Class using a declaration -
i'm implementing automatak opendnp3 libraries c++. want add master channel.as can see definition, parameter action is:
... opendnp3::imasterapplication& appliction, ...
the imasterapplication interface described here. create masterapplication.cpp , masterapplication.h files , attempt implement class follows:
masterapplication.cpp
#include "masterapplication.h" #include <opendnp3/master/imasterapplication.h> #include <iostream> #include <chrono> using namespace opendnp3; using namespace asiodnp3; using namespace std; masterapplication::masterapplication() { } masterapplication::~masterapplication() { }
masterapplication.h
#ifndef masterapplication_h_ #define masterapplication_h_ #include <opendnp3/master/imasterapplication.h> #include <opendnp3/link/ilinklistener.h> #include <openpal/executor/iutctimesource.h> class masterapplication : public opendnp3::imasterapplication { private: public: masterapplication(); virtual ~masterapplication(); }; #endif
but, when try declare masterapplication object in main use:
masterapplication imaster;
and place within addmaster function, error:
main.cpp:57:20: error: cannot declare variable ‘imaster’ of abstract type ‘masterapplication’ masterapplication imaster;
what misunderstanding here?
as mentioned in comment, whenever implementing abstract class (interface), pure virtual methods part of abstract class must defined (provided body) in derived class. derived class becomes concrete, , can used type. otherwise becomes abstract.
"so declaration of method make non-abstract?"
not declaration, definition. methods declared (as pure virtual) in base class (interface). giving them definition (body) in derived class makes derived class non-abstract (concrete).
"also, have declare in masterapplication without using it? "
you need define pure virtual methods of base class in derived class. use them or not depend on use-case.
Comments
Post a Comment