Solved

Why am I getting a "pure virtual function has no overrider" Microsoft VS C++ compiler error when compiling my COM object?

Posted on 2014-12-09
11
651 Views
Last Modified: 2014-12-15
Hi:

I'm building a simple Microsoft VS C++ based COM object.
I've run into an compile time error I can't quite figure out:

Here is the definition in the header file:

struct __declspec(uuid("xxx")) IFooBar : IUnknown
{
      virtual bool __stdcall GetValues( std::vector<int>& values ) 
}

Open in new window


Here is the definition in the CPP file:
struct FooBar : IFooBar
{
      bool __stdcall GetValues( std::vector<int>& values ) 
      {
            return true;
      }
}

Open in new window


The idea behind the method is to return a vector/array or list (collection) to the caller.

At compile time I get the following error:
IntelliSense: object of abstract class type "FooBar" is not allowed:
   pure virtual function "IFooBar::GetValues" has no overrider.
I'm not sure what I'm doing wrong here.
I know I can pass a const vector of int's into the COM object.
Is there a better way to do this?
I'm really trying to basically return a list (collection) of integers back to the caller.

Thanks,
JohnB
0
Comment
Question by:jxbma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 34

Expert Comment

by:sarabande
ID: 40491069
the 'Intellisense' is not a compiler. if you would compile a cpp file which included the header you would get other errors as there are missing ; both at end of declaration of the virtual function and at end of struct definition.

a virtual function would be declared in a baseclass. in the derived class (or struct)  you would only be able to override existing virtual functions of the baseclass or declare new virtual functions that could be overridden by classes that were derived from your class. if a baseclass does not implement a virtual function but adds '= 0' at end of declaration, the virtual function is called a 'pure virtual function' cause any derived class must provide an implementation of the function. if it doesn't, it is not possible to create objects of the derived class because of the missing function. because of this, a class that has at least one pure virtual function, is called an 'abstract' class as you can't make instances of the class itself but only of derived classes.

Sara
0
 
LVL 34

Expert Comment

by:sarabande
ID: 40491083
try

// headerfile

struct __declspec(uuid("xxx")) IFooBar : IUnknown
{
      virtual bool __stdcall GetValues( std::vector<int>& values ) = 0;
};

struct FooBar : IFooBar
{
      bool __stdcall GetValues( std::vector<int>& values ) 
      {
            return true;
      }
};  // note, the semicolon


// cpp file 
#include "headerfile"

...
    FooBar foo;  // should compile now

Open in new window


Sara
0
 
LVL 1

Author Comment

by:jxbma
ID: 40491410
I think you may be missing the point on this question.
I understand the mechanics of base classes and virtual functions.
I have several other virtual functions declared in my "Interface" and defined in my "struct".

I'm building a COM object here.

The issue seems to be this parameter definition:
std::vector<int>& values

Open in new window


If I change it to a const, then it compiles fine.
const std::vector<int>& values

Open in new window


So perhaps the question is what do I need that definition to be to pass a reference to a vector or array in.
I need the method in question to return a collection of integer (in this case) values.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Expert Comment

by:sarabande
ID: 40491924
'Intellisense' is not a compiler. because of that the message could be wrong (for example if Intellisense was not able to recognize that you want to add a new virtual function rather than overriding an existing one).

if the message is not wrong, the compiler would output a similar error message, which might give better hint what is wrong. perhaps it is not the FooBar which makes the trouble but the IFooBar which is not allowed to be an abstract class.

I think you may be missing the point on this question.
yes, that is possible. but I am dependent on you to post valid code to make a valid analysis. if you post declarations which are not terminated by a semicolon (and therefore do not compile), I have good reason to assume that you didn't post the whole story but only parts where you think they are causing the issue. that is not a good base to start with.

If I change it to a const, then it compiles fine
can you post the compiler error? and the code line that caused the error?

Sara
0
 
LVL 1

Author Comment

by:jxbma
ID: 40491975
Code was already posted in the original posting.

Here are the specific errors that I previously described:
111	error C2259: FooBar : cannot instantiate abstract class	C:\FooBar .cpp	170	1	FooBar 

112	IntelliSense: object of abstract class type "Foobar" is not allowed:
            pure virtual function "IFooBar::GetValues" has no overrider	c:\FooBar.cpp	170	31	FooBar

Open in new window


Please reread my comments in detail.
The (seems to me) to be using the std::vector parameter in the method contract in IFooBar.
If I change it to something else, it compiles fine.

JB
0
 
LVL 34

Expert Comment

by:sarabande
ID: 40492163
Code was already posted in the original posting.
as told the code in the original post is incomplete and would not compile. there are missing semicolons and the function is not pure virtual.



If I change it to something else, it compiles fine.
where do you change it? in the IFooBar or in the FooBar or in both?

if you would post the headers and the relevant piece of code around line 170 in foobar.cpp, I probably could solve the issue?

Sara
0
 
LVL 1

Author Comment

by:jxbma
ID: 40492490
Dude. I will figure this out myself. Thanks.
0
 
LVL 34

Accepted Solution

by:
sarabande earned 500 total points
ID: 40493395
i tried to compile the code snippet i posted above and got the following output

>error C2259: 'FooBar' : cannot instantiate abstract class
1>          due to following members:
1>          'HRESULT IUnknown::QueryInterface(const IID &,void **)' : is abstract
1>          ..\include\unknwn.h(116) : see declaration of 'IUnknown::QueryInterface'
1>          'ULONG IUnknown::AddRef(void)' : is abstract
1>          ..\include\unknwn.h(120) : see declaration of 'IUnknown::AddRef'
1>          'ULONG IUnknown::Release(void)' : is abstract
1>          ..\unknwn.h(122) : see declaration of 'IUnknown::Release'

Open in new window


so my code gives the same error as the one you posted but due to missing implementations of virtual functions QueryInterface, AddRef, and Release declared pure virtual in IUnknown.

Sara
0
 
LVL 1

Author Comment

by:jxbma
ID: 40499301
I wasn't going to paste the entire interface, nor was I going to paste a small example that illustrates the issue.
I figure by looking at the error message and the several lines of code one could tell me what's going on.

The issue was clearly something to do the parameter definition:
   
"std::vector<int>& values "

Open in new window


Of course you got compiler errors for the missing implementations of QueryInterface, AddRef, and Release.

I figured it out on my own and went another direction.
0
 
LVL 1

Author Closing Comment

by:jxbma
ID: 40499304
Not a very productive interaction for me.
0
 
LVL 34

Expert Comment

by:sarabande
ID: 40499903
I wasn't going to paste the entire interface, nor was I going to paste a small example that illustrates the issue.
indeed. you did nothing and therefore you got nothing.

The issue was clearly something to do the parameter definition: "std::vector<int>& values"
if you were right, the error messages would have pointed to that definition. in the code i compiled the argument was accepted by the compiler without errors.

I figured it out on my own and went another direction.
that's ok. you should accept your own comment then. or better the question should be deleted as it of no value for anyone. please, click the 'request attention' button below your original post and ask a moderator to delete the question.

Sara
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need some help with Microsoft Visual Studio C++ 2003 5 73
White board coding practice 3 95
Trouble with References... 5 63
Problem to open text file 11 136
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

733 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