开发者

GCC problem - invalid conversion from 'void (*)(MyObject*, bool)' to 'const void*'

I really don't want to, but I have to emulate COM logic in my program and I'm using standard COM_ADDREF macros, but I keep getting the following error: invalid conversion from 'void ()(MyObject, bool)' to 'const void*'... What should I do?

#define COM_ADDREF(pObj, pMaster) ((pObj)->AddRef((pMaster), __FILE__, __LINE__, pObj))

class BaseComObject
{
public:
    inline DWORD AddRef (const void* pMaster, const char* pFileName, int line, const void* pObj) const
    {
        iRefCount++;
        return iRefCount;
    };

    inline DWORD G开发者_JAVA技巧etRefCount() const
    {
        return iRefCount;
    };

private:
    long iRefCount;
};

class MyObject: public BaseComObject { };

void test (MyObject* pObject, bool bValue)
{
    if (pObject)
    {
        COM_ADDREF (pObject, bValue);// error: invalid conversion from 'void (*)(MyObject*, bool)' to 'const void*'
    }
}

error: invalid conversion from 'void ()(MyObject, bool)' to 'const void*'

error: initializing argument 1 of 'DWORD BaseComObject::AddRef(const void*, const char*, int, const void*) const'


It doesn't look to me like this is the code that's generating that error. When I compile it with g++ (fixing the obvious errors like DWORD and modifying class state in a const function, it just tells me it can't find a matching function.

The gist is you're passing a bool as the second argument to COM_ADDREF and the base class AddRef function actually wants a void* in that parameter position. I highly suspect you have a class with an implicit bool constructor you aren't showing us, and that's really confusing the compiler.


Are you posting the same code you compiled?

The error message "invalid conversion from void (*)(MyObject*, bool) ... " clearly refers to the (address of the) function void test (MyObject* pObject, bool bValue) in this example. But you don't refer to test on the line that should have the error.


Function pointers such as void ()(MyObject, bool) and data pointers such as void * are not guaranteed to be the same size (certainly not by the C standard; I assume not by the C++ standard either). POSIX does grant that guarantee, but the language standards do not.

Sometimes, you can get around the problem with a union:

union DataFunction
{
    void  *data;
    void (*function)();
};

You can assign to the function member and read from the data member and even though the standard does not guarantee that will work, unless sizeof(void *) != sizeof(void (*)()), it will work in practice.


The code you posted does not match the description/messages you provided. I highly doubt one can get these error message from this code.

You code makes an obvious attempt to pass a bool argument for a const void * parameter, which is indeed an error: you are passing bool bValue argument as const void* pMaster parameter to AddRef. But the diagnostic message for this error should be completely different.

My guess is that you posted wrong code.

BTW, the part of COM logic you are trying to emulate (at least so far) is called reference counting. It has a value of its own, not attached specifically to COM. You can find quite a few examples of well-implemented reference counting in C++ on the Net.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜