cannot instantiate abstract class due to following members

Posted on 2005-05-17
Last Modified: 2013-11-25
I've created a COM component with 2 CoClasses Cbar and Cfoo.  I then add a property to Ibar, called foobar, specifying a Property type of Ifoo *.  Now normally if I defined a property of type int, long etc., when I did a build I would end up with
STDMETHODIMP Cbar::get_abcbar(short *pVal) { // TODO: Add your implementation code here  return S_OK; }

STDMETHODIMP Cbar::put_abcbar(short newVal) { // TODO: Add your implementation code here return S_OK;}
in bar.cpp.
However this doesn't happen when I add my property of type Ifoo*.  Consequently when I compile I get the error
"cannot instantiate abstract class due to following members"
:get_foobar(struct Ifoo ** )' : pure virtual function was not defined
Here is the IDL
            helpstring("Ifoo Interface"),
      interface Ifoo : IDispatch
            helpstring("Ibar Interface"),
      interface Ibar : IDispatch
            [propget, id(1), helpstring("property foobar")] HRESULT foobar([out, retval] Ifoo* *pVal);
            [propput, id(1), helpstring("property foobar")] HRESULT foobar([in] Ifoo* newVal);
Question by:ascot
  • 2

Accepted Solution

rcarlan earned 250 total points
ID: 14022227
You haven't specified how you're adding the properties to your COM interfaces. I assume you're using the ATL Wizard in Visual Studio - i.e. you right-click on the Ibar interface in the ClassView browser and choose Add Property.

If this is the case, the ATL Wizard should:
1. add the properties to the IDL file (a propget and a propput)
2. add the corresponding method declarations to the C++ class that implements the COM object
3. add a bogus implementation for these methods in the cpp file containing the implementation of the C++ class

Seems to me like 1, and possibly 2, are happening, but definitely not 3.

Unfortunately, the ATL Wizard does have its quirks and, while I have never experienced this particular problem myself, I’ve certainly had my share of problems with the ATL Wizard.

It’s possible that somebody may be able to help you out with the changes you would have to make to your file(s) or project to get the ATL Wizard to work properly. However, in the mean time, you can get over this hurdle by manually updating the C++ class that implements the Ibar interface.

You have two options:

1. Having used the ATL Wizard to add the Ifoo property, open the header file corresponding to the C++ class that implements the COM object and see if the ATL Wizard has added the two methods (e.g. get_foobar and put_foobar). If it hasn’t, add the following lines:
STDMETHOD(get_foobar)(/*[out, retval]*/ Ifoo** ppIfoo);
STDMETHOD(put_foobar)(/*[in]*/ Ifoo* pIfoo);

Then, open the cpp file corresponding to C++ class implementation and add:

STDMETHODIMP Cbar::get_foobar(Ifoo** ppIfoo)
      return E_NOTIMPL;

STDMETHODIMP Cbar::put_foobar(Ifoo* pIfoo)
      return E_NOTIMPL;

2. Alternatively, since you said that adding long properties works as expected, add a long property instead of Ifoo (you may want to also give IUnknown a try) and then manually edit the IDL, h and cpp files to change the property’s type to Ifoo.

None of these solutions are ideal, as they do require tedious manual labour, but at least you’ll be able to move on with your work.



Author Comment

ID: 14025317
Thanks for the input.  Tried again this morning to see if it was the order that I was creating my ATL objects that was causing the problem.
First of all did the following:-
Create project using ATL COM Wizard.
Create DLL.
Right click on Project folder - NEW ATL Object - Simple Object - Short Name "foo"
Right click on Project folder - NEW ATL Object - Simple Object - Short Name "bar"
In Class View, right click on Ibar and select "Add Property.  Specify Prperty Type as Ifoo*, Property Name foobar.

Every built ok.

So then tried

Create project using ATL COM Wizard.
Create DLL.
Right click on Project folder - NEW ATL Object - Simple Object - Short Name "bar"
Right click on Project folder - NEW ATL Object - Simple Object - Short Name "foo"
In Class View, right click on Ibar and select "Add Property.  Specify Prperty Type as Ifoo*, Property Name foobar.

When I build I get the error
error MIDL2025 : syntax error : expecting a type specification near "Ifoo"
error MIDL2026 : cannot recover from earlier syntax errors; aborting compilation  

so I edited the IDL file to move the Ifoo interface definition before the Ibar interface definition and everything built ok.  
So I am at a loss to understand why things failed previously.

But again thanks for a very clear response which did help me understand the problem.

Expert Comment

ID: 14025568
I've been doing MFC and ATL programming for longer than I care to remember. OK, I'm going to admit it: I started with Visual C++ (that is, the first version, 16 bit, Win 3.1, back in 1993) and I've been doing it ever since. It doesn't get any longer than that :-)

Anyway, with the experience I have I can say this: the Wizards are great (at least the MFC Wizard, 'cause the ATL one is pretty useless;-) but you have to understand what it is that they do. There's no magic (despite the fact they're called Wizards:-). All they do is to insert code snippets. Once you understand exactly what code you get from them and under what circumstances, you become much better at using them (or not using them - as the case may be;-). You have to become comfortable with editing the code they generate, with typing the stuff in yourself, etc.

Documentation helps, searching on the NET, asking questions on Experts Exchange ;-) but the best tool is a source control system (even if it's a personal one, with the repository on your local machine). Check the files out, invoke a Wizard command, and do a "Show Differences". Doesn't get much clearer than that.

I'm sure you've already learned a lot about the ATL Wizard through this exercise. You had the right approach: follow a sequence of steps and analyse the results.

Good luck in your programming career.


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Image decoding from Camera 3 72
How to make an ADE file by code? 11 79
Problem to With line 4 39
"Black Box" Testing of Control System Software 2 21
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now