문제 클래스가 *.h
과 *.cpp files
으로 나뉘어 질 때 일부 추상 클래스에서 상속 된 순수 가상 함수를 구현하는 데 문제가 있습니다. 컴파일러 (g++
)는 순수 함수가 있기 때문에 파생 클래스를 인스턴스화 할 수 없다고 알려줍니다. 인터페이스의 *.h
과 너무 derived.h
에 - C++의 순수 가상 함수 구현 및 헤더 파일
/** interface.h**/
namespace ns
{
class Interface {
public:
virtual void method()=0;
}
}
/** interface.cpp**/
namespace ns
{
//Interface::method()() //not implemented here
}
/** derived.h **/
namespace ns
{
class Derived : public Interface {
//note - see below
}
}
/** derived.cpp **/
namespace ns
{
void Derived::Interface::method() { /*doSomething*/ }
}
/** main.cpp **/
using namespace ns;
int main()
{
Interface* instance = new Derived; //compiler error
}
이 나는 회) ( 방법을 선언
해야한다는 뜻인가요? 주위에 다른 방법이 없습니까?
이것은 C++가 아닙니다. 'public void method() = 0;'자바와 비슷하게 보입니다. –
첫 번째로 .cpp 파일을 가질 이유가 없으며 유일한 네임 스페이스는 공백 네임 스페이스입니다. 추상 클래스를 처리하는 헤더에 대한 구현 파일은 필요하지 않습니다. – Falmarri
@Falmarri가 정확합니다. –