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
542 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
  • 6
  • 5
11 Comments
 
LVL 32

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 32

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
 
LVL 32

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 32

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 32

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 32

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

746 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

11 Experts available now in Live!

Get 1:1 Help Now