my problem is: How do I implement pure virtual functions in an inherited class? It always says i didn't implement the only function, but I tried to do it. So where is my mistake?
My code:
A.h:
class A {
public:
A();
virtual std::pair<A*, A*> f1(const A& o) const=0;
virtual ~A();
};
B.h:
#include "A.h"
class B : public A {
public:
B();
virtual ~B();
virtual std::pair<A*, A*> f1(const A& o);
};
B.cpp:
#include "B.h"
B::B() : A() {}
B::~B() {}
std::pair<A*, A*> B::f1(const A& o) {
A* K1=new B();
A* K2=new B();
return std::make_pair (K1, K2);
}
I get following error:
B.cpp: In member function ‘virtual std::pair<A*, A*> B::f1(const A&)’:
B.cpp:14:16: error: cannot allocate an object of abstract type ‘B’
A* K1=new B();
^
In file included from B.cpp:1:0:
B.h:4:7: note: because the following virtual functions are pure within ‘B’:
class B : public A {
^
In file included from B.h:1:0,
from B.cpp:1:
A.h:10:28: note: virtual std::pair<A*, A*> A::f1(const A&) const
virtual std::pair<A*, A*> f1(const A& o) const=0;
^
B.cpp:15:16: error: cannot allocate an object of abstract type ‘B’
A* K2=new B();
^
In file included from B.cpp:1:0:
B.h:4:7: note: since type ‘B’ has pure virtual functions
class B : public A {
^
Also: What is correct, A* K1=new A(); or new B(); ?
A::f1is aconstmethod. You forgot theconstat the end of the method in classB.overridekeyword is awesome.