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
558 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 33

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 33

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 33

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
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.

 
LVL 33

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 33

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 33

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

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

912 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

16 Experts available now in Live!

Get 1:1 Help Now