我想在「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