2011-10-01 90 views
1

我想在“Dog”类中实现一个接口,但出现以下错误。最终目标是使用一个接收可比对象的函数,以便它可以将对象的实际实例与我通过参数传递的对象进行比较,就像等于一样。运算符重载不是一个选项,因为我必须实现该接口。 使用“新”关键字创建对象时会触发错误。无法在C++错误中实例化抽象类

“错误2错误C2259: '狗':不能实例化抽象类C:\用户\菲尼克斯\文档\ Visual Studio 2008的\项目\接口测试\接口测试\接口TEST.CPP 8”

这里是所涉及的类的代码:

#pragma once 

class IComp 
{ 
    public: 
     virtual bool f(const IComp& ic)=0; //pure virtual function 
}; 

    #include "IComp.h" 
class Dog : public IComp 
{ 
    public: 
     Dog(void); 
     ~Dog(void); 
     bool f(const Dog& d); 
}; 

#include "StdAfx.h" 
#include "Dog.h" 

Dog::Dog(void) 
{ 
} 

Dog::~Dog(void) 
{ 
} 

bool Dog::f(const Dog &d) 
{ 
    return true; 
    } 

#include "stdafx.h" 
#include <iostream> 
#include "Dog.h" 

using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    Dog *d = new Dog; //--------------ERROR HERE** 

    system("pause"); 
     return 0; 
} 

回答

3

bool f(const Dog &d)不是bool f(const IComp& ic)的实施,使虚拟bool f(const IComp& ic)仍然没有被Dog

+0

谢谢你,现在工作。 – HoNgOuRu

1

你Dog类不实现方法F,因为它们有不同的签名。它也需要在Dog类中声明为:bool f(const IComp& d);,因为bool f(const Dog& d);是另一种方法。

+0

谢谢你的回答 – HoNgOuRu

1
bool f(const Dog& d); 

实施不是为IComp

virtual bool f(const IComp& ic)=0; //pure virtual function 

你的Dog定义“的实现s f实际上是隐藏纯虚函数,而不是实现它。

+0

谢谢,现在正在工作,我得等4分钟才能接受你的回答。 – HoNgOuRu

相关问题