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
594 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

831 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