2011-03-10 95 views
1

继承我有两个类是这样的:VC++从抽象类

// parent.h 

class Parent { 
    public: 
     virtual void Method() = 0; 
} 

//child.h 

#include "parent.h" 

class Child : public Parent { 
    public: 
     Child(); 
     ~Child(); 
     virtual void Method(); 
} 

//child.cpp 

#include "child.h" 

Child::Child() { } 
Child::~Child() { } 

void Child::Method() { } 

+

void main() { 
    Parent* p = new Child(); 
} 

这个工程˚F在Linux上使用g ++,但是当我尝试在VS 2010中应用相同的模式时,我得到:

error C2259: 'Child' : cannot instantiate abstract class 

任何想法为什么?

+1

你用g ++和VS2010编译完全相同的文件吗?当然,这不是一个错字? g ++行为是正确的。 – Erik 2011-03-10 21:27:10

+0

我把这些文件重新组合成一个新的项目,一切都很顺利,所以我猜想有一个错误的地方。你是对的 – Gabriel 2011-03-10 22:54:53

回答

0

我认为您需要在头文件中的函数签名之前添加public:。 g ++可能会将其解释为私​​有函数。

+0

它是公开的,我错了,也有一个构造函数和析构函数定义在头文件 – Gabriel 2011-03-10 21:25:53