I want to implement an interface inside a “Dog” class, but I’m getting the following error. The final goal is to use a function that recieves a comparable object so it can compare the actual instance of the object to the one I’m passing by parameter, just like an equals. An Operator overload is not an option cause I have to implement that interface.
The error triggers when creating the object using the “new” keyword.
” Error 2 error C2259: ‘Dog’ : cannot instantiate abstract class c:\users\fenix\documents\visual studio 2008\projects\interface-test\interface-test\interface-test.cpp 8 “
Here is the code of the classes involved:
#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;
}
bool f(const Dog &d)is not an implementation ofbool f(const IComp& ic), so the virtualbool f(const IComp& ic)still isn’t implemented byDog