我想在“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;
}
谢谢你,现在工作。 – HoNgOuRu