Instantiate abstract class
Hi all,
I have a class, CCollection, which holds a number of CConnection objects. CCollection and CConnection are interfaces for a COM object.
I want to use pointers, but when I want to create a new object, I receive this error:
error C2259: 'CConnection' : cannot instantiate abstract class due to following members:
warning C4259: 'long __stdcall CConnection::QueryInterface(const struct _GUID &,void ** )' : pure virtual function was not defined
It happens with this code:
CConnection * pTemp = new CConnection(parameters);
I looked up in the help what this error means, but it still isn't clear to me. If I try to overwrite this function, the compiler says the function is already declared.
Any idea how to fix this error??
Thanks!
Re: Instantiate abstract class
You cannot instantiate an abstract class as you already experience...you need to provide implementations for all pure virtual functions....for further information take a look at the following FAQs:
Re: Instantiate abstract class
Quote:
Originally Posted by Andreas Masur
You cannot instantiate an abstract class as you already experience...you need to provide implementations for all pure virtual functions....for further information take a look at the following FAQs:
So, when I implement those functions, I am able to instantiate the class? Or do I understand you wrong?
If I am right, how and where can I implement those functions, because when I am trying to overwrite them, the compiler says I have already defined the function.
I'll take a look at the links you gave me! Thanks!
Re: Instantiate abstract class
Quote:
Originally Posted by Tischnoetentoet
If I am right, how and where can I implement those functions, because when I am trying to overwrite them, the compiler says I have already defined the function.
The pure virtual functions are to be implemented in derived classes. If one derived class ( regardless of its level in the hierarchy) implements all the pure virtual functions it becomes a concrete class and can be instantiated. Otherwise it is an abstract class and cannot be instantiated.
Re: Instantiate abstract class
One more point ....
Here the class being instantiated is COM object, so you need to implement the provided abstract functions within the class .
AddRef()
QueryInterface()
....
Re: Instantiate abstract class
Thanks a lot!
I'll try to fix it! Thanks!
Re: Instantiate abstract class
Ok, here is what I did:
I added this code to my CConnection class to implement the functions:
Code:
unsigned long CConnection::AddRef()
{
return 0;
}
//=====================================================================
HRESULT CConnection::QueryInterface(REFIID riid, void** ppv)
{
// Call original function
return S_OK;//IConnection::QueryInterface(riid, ppv);
}
//=====================================================================
ULONG CConnection::Release()
{
return 0;
}
These are the errors I get now:
Code:
Collection.cpp(105) : error C2259: 'CConnection' : cannot instantiate abstract class due to following members:
connection.h(27) : see declaration of 'CConnection'
Collection.cpp(105) : warning C4259: 'long __stdcall CConnection::QueryInterface(const struct _GUID &,void ** )' : pure virtual function was not defined
connection.h(43) : see declaration of 'QueryInterface'
Collection.cpp(105) : warning C4259: 'unsigned long __stdcall CConnection::AddRef(void)' : pure virtual function was not defined
connection.h(43) : see declaration of 'AddRef'
Collection.cpp(105) : warning C4259: 'unsigned long __stdcall CConnection::Release(void)' : pure virtual function was not defined
connection.h(43) : see declaration of 'Release'
Collection.cpp(107) : error C2259: 'CConnection' : cannot instantiate abstract class due to following members:
connection.h(27) : see declaration of 'CConnection'
Collection.cpp(107) : warning C4259: 'long __stdcall CConnection::QueryInterface(const struct _GUID &,void ** )' : pure virtual function was not defined
connection.h(43) : see declaration of 'QueryInterface'
Collection.cpp(107) : warning C4259: 'unsigned long __stdcall CConnection::AddRef(void)' : pure virtual function was not defined
connection.h(43) : see declaration of 'AddRef'
Collection.cpp(107) : warning C4259: 'unsigned long __stdcall CConnection::Release(void)' : pure virtual function was not defined
connection.h(43) : see declaration of 'Release'
Any idea what's wrong now??
Re: Instantiate abstract class
You never told us you were trying to write a COM object !! I presume this is inside of a dll :
Use the ATL wizards to create COM objects and add methods to them. Don't try to do them by hand unless you REALLY know what you're doing.
What exactly is CConnection ? I take it you've got an IConnection interface.
Darwen.
Re: Instantiate abstract class
Quote:
What exactly is CConnection ? I take it you've got an IConnection interface.
Yes, you are right! It is an interface. A short description what my COM object must do:
I want a connection with a PLC (Programmable Logic Controller). The communication for 1 PLC is done by CConnection.
I have made a collection class (CCollection) which can hold up to 128 CConnection objects. This way the user is able to connect with several PLC's with one COM object.
I can communicate with my COM object (ICollection). But when I want to add a new object of CConnection to the array of connections, these errors occurres...
I hope it's clear to you now!
Thanks so far!
Re: Instantiate abstract class
CConnection is an abstract base class. You cannot instantiate it. You need to derive a class from it and override the pure virtual functions in your derived class.
Re: Instantiate abstract class
Quote:
Originally Posted by GCDEF
CConnection is an abstract base class. You cannot instantiate it. You need to derive a class from it and override the pure virtual functions in your derived class.
After implementing the abstract function in your derived class, you can instantiate the derived class, and make use of that.
Re: Instantiate abstract class
But I didn't even declare the functions AddRef, Release and QueryInterface myself!
Why would a class derived from CConnection work, but CConnection itself not?
Maybe you can explain me?
Re: Instantiate abstract class
Quote:
Originally Posted by Tischnoetentoet
But I didn't even declare the functions AddRef, Release and QueryInterface myself!
Why would a class derived from CConnection work, but CConnection itself not?
Maybe you can explain me?
That's the way pure virtual functions and abstract base classes work. It's part of the language.
Re: Instantiate abstract class
so if I do this, it should work?
Code:
class CConnectionImpl : public CConnection
{
public:
// Constructor & destructor
CConnectionImpl();
~CConnectionImpl();
// Functions
// Variables
private:
// Functions
HRESULT QueryInterface(RIID, void**)
{
// Implementation
}
ULONG AddRef()
{
// Implementation
}
ULONG Release()
{
// Implementation
}
// Variables
}
Just copy the whole class, but implement the 3 functions. Is that all?
Re: Instantiate abstract class
Quote:
Originally Posted by Tischnoetentoet
so if I do this, it should work?
Code:
class CConnectionImpl : public CConnection
{
public:
// Constructor & destructor
CConnectionImpl();
~CConnectionImpl();
// Functions
// Variables
private:
// Functions
HRESULT QueryInterface(RIID, void**)
{
// Implementation
}
ULONG AddRef()
{
// Implementation
}
ULONG Release()
{
// Implementation
}
// Variables
}
Just copy the whole class, but implement the 3 functions. Is that all?
Not sure what you mean "copy the whole class". You create a new class derived from CConnection and implement only the functions you need to, which in this case seems to be the pure vitrual ones. You don't need to recreate all the variables and other functions whose behavior you don't want to change.