?
Solved

data conversion error when assigning function to function pointer

Posted on 2007-03-21
7
Medium Priority
?
802 Views
Last Modified: 2013-12-08
Hi Experts,
    I am trying to assign a function to a function pointer member in CFactoryTemplate, which is a class from directshow SDK

this is the class that i wrote:
class CMYFilter: public CTransInPlaceFilter
{
    public:
        CUnknown * WINAPI CreateInstance(LPUNKNOWN pUnk, HRESULT *pHr);
};

What i am trying to do is assign CreateInstance to a member of type
LPFNNewCOMObject
which has its original declaration as
typedef CUnknown *(CALLBACK *LPFNNewCOMObject)(LPUNKNOWN pUnkOuter, HRESULT *phr);

the assignment is shown here:
CFactoryTemplate g_Templates[1] =
{
  {
    g_wszName,
    &CLSID_CMYFilter,
    CMYFilter::CreateInstance,  // line with error
    NULL,
    NULL
  }
};

I kept receiving this error message at the line CMYFilter::CreateInstance.

error C2440: 'initializing' : cannot convert from 'CUnknown *(__cdecl
CMYFilter::* )(LPUNKNOWN,HRESULT *)' to 'LPFNNewCOMObject'
1>        None of the functions with this name in scope match the target type

This is very strange since all the sample codes, including those came with
SDK, were written like this.

Is there any reason why this is happening?
thanks!
0
Comment
Question by:fungi8210
[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
  • 5
  • 2
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 18766235
You need to make that function a 'static' member of your class - non static ones are passed an invisible 'this' pointer which makes the signature different, thus the error.

class CMYFilter: public CTransInPlaceFilter
{
    public:
        static CUnknown * WINAPI CreateInstance(LPUNKNOWN pUnk, HRESULT *pHr);
};

will get that to work.
0
 
LVL 86

Expert Comment

by:jkr
ID: 18766237
BTW, a factory function also does not need to be a non-static member.
0
 

Author Comment

by:fungi8210
ID: 18766258
i just added static to it, now i am getting these errors
error LNK2001: unresolved external symbol "public: static class CUnknown * __cdecl CFPSFilter::CreateInstance(struct IUnknown *,long *)" (?CreateInstance@CFPSFilter@@SAPAVCUnknown@@PAUIUnknown@@PAJ@Z)

error LNK2001: unresolved external symbol CLSID_FPSFilter
1>Windows Mobile 6 Professional SDK (ARMV4I)\Debug/MyFilter.dll : fatal error LNK1120: 2 unresolved externals

just incase you are wondering, i'm trying to write a directshow filter, and CLSID_FPSFilter is my classid defined at the top

// {033104B2-2AA9-4476-992E-AB504F9F1F1E}
DEFINE_GUID(CLSID_FPSFilter,
0x33104b2, 0x2aa9, 0x4476, 0x99, 0x2e, 0xab, 0x50, 0x4f, 0x9f, 0x1f, 0x1e);

btw, you answered my question correctly, you'll get the points, just wondering if you know how to solve my second problem, thanks!
0
Industry Leaders: 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 86

Expert Comment

by:jkr
ID: 18766283
You'll need to provide an implementation also, e.g.

static CUnknown * WINAPI CMYFilter::CreateInstance(LPUNKNOWN pUnk, HRESULT *pHr) {

  CMYFilter* p = NULL;

  // other code to create a 'CMYFilter' here...

  return p;
}

0
 
LVL 86

Expert Comment

by:jkr
ID: 18766293
Sorry, that should have been

CUnknown * WINAPI CMYFilter::CreateInstance(LPUNKNOWN pUnk, HRESULT *pHr) {

  CMYFilter* p = NULL;

  // other code to create a 'CMYFilter' here...

  return (CUnknown*) p;
}
0
 

Author Comment

by:fungi8210
ID: 18766319
thanks, one last error

CMYFilter.obj : error LNK2001: unresolved external symbol CLSID_CMYFilter
1>Windows Mobile 6 Professional SDK (ARMV4I)\Debug/MyFilter.dll : fatal error LNK1120: 1 unresolved externals

its weird because i defined CLSID_CMYFilter at the top


// {033104B2-2AA9-4476-992E-AB504F9F1F1E}
DEFINE_GUID(CLSID_CMYFilter,
0x33104b2, 0x2aa9, 0x4476, 0x99, 0x2e, 0xab, 0x50, 0x4f, 0x9f, 0x1f, 0x1e);

0
 
LVL 86

Expert Comment

by:jkr
ID: 18766368
>>its weird because i defined CLSID_CMYFilter at the top

Yes, but for that to be expanded to also an implementation (not just a declaration) you'll need to

#define INITGUID

before

#include <objbase.h>
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

777 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