내 문제는 : 어떻게 상속 된 클래스에서 순수 가상 함수를 구현합니까? 그것은 항상 내가 유일한 기능을 구현하지 않았다고 말하지만 그것을하려고했습니다. 그럼 내 실수는 어디 있니?C++ 상속 순수 가상 함수
내 코드 :
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);
}
나는 다음과 같은 얻을 오류 :
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 {
^
기타 : A * K1 = new A(); 또는 새로운 B(); ?
'A :: f1'는'const' 메소드입니다. 'B' 클래스의 메소드 끝 부분에서'const'를 잊어 버렸습니다. – crayzeewulf
그래서'override' 키워드가 굉장합니다. – Jarod42