Link to home
Start Free TrialLog in
Avatar of 12bsure
12bsure

asked on

Why can't the VC++ compiler choose the right overloaded method?

Hi,

In atlcom.h a class called CComDispatchDriver is declared and implemented. It is quite handy to use, yet I wondered why noone ever mentioned (not even in the documentation) the InvokeX range of functions. But maybe the following story is why: I have this piece of code, that works perfectly, and another, just below that one, that doesn't. I want to know why it compiles faulty.

STDMETHODIMP CFastGrid::_CB_Click(IDispatch *CallBackObj, long Row, long Col)
{
    if(!m_pdispCallback)
        return S_OK;

    CComDispatchDriver cb(m_pdispCallback);
   
    //InvokeN inverts the array: arguments are in reverse order!!!
    CComVariant varArgs[3] = {Col, Row, CallBackObj};
    //Mind you: the article on DevX is wrong: don't use sizeof in the third param!!!
    cb.InvokeN(m_bstrCallBackPrefix + "Click", varArgs, 3);        
    return S_OK;
}

//Test the test callback function manually
STDMETHODIMP CFastGrid::_CB_Test()
{
    if(!m_pdispCallback)
        return S_OK;

    CComDispatchDriver cb(m_pdispCallback);
    CComVariant var(1);
    cb.Invoke1(m_bstrCallBackPrefix + "Click", var);
    return S_OK;
}

These functions work well as long as I use either Invoke0 or InvokeN. For some reason Invoke1 and Invoke2 don't seem to work. The error I get is:

error C2664: 'long __thiscall ATL::CComDispatchDriver::Invoke1(long,struct tagVARIANT *,struct tagVARIANT *)' : cannot convert parameter 1 from 'class _bstr_t' to 'long'

which is a very general error and actually means that the second overloaded method, having its first parameter defined as an LPCOLESTR, is not found by the compiler. I get a similar error using L"somestring" as argument. When I look up the declarations, all the functions are there and the other ones, Invoke0 and InvokeN use exactly the same way of calling (also an LPCOLESTR argument). What is happening here? Is this the compiler's fault, or does the atlcom.h have an error? I couldn't find any in this part.

Although I can overcome this error by just sticking to Invoke0 and InvokeN, I had this error before (and solved it back then). I just want to know what is happening here and what the compiler is "thinking".

If the problem appears to be more difficult than I initially thought, I'll increase the points accordingly.

Thanks in advance,
12Bsure corp.
Avatar of DanRollins
DanRollins
Flag of United States of America image

The compiler will always complain if it can't find a match for any overload.  

Show how you declared

  m_bstrCallBackPrefix

Do you have the same problem if you simply provide some coersion to let the compiler know which override you want?:
  cb.Invoke1((LPCOLESTR)(m_bstrCallBackPrefix + "Click"), var);

Do you have the same problem if you don't try to add two strings?  For instance, does...

  cb.Invoke1( m_bstrCallBackPrefix, var);

work?  The result of the += operator is a bstr_t&

-- Dan
Avatar of 12bsure
12bsure

ASKER

//This is the declaration line:
CComBSTR    m_bstrCallBackPrefix;

//This is the initialization line (in the constructor):
m_bstrCallBackPrefix     =  "CB_";

//And fyi, These are the declarations of all the InvokeX functions:
HRESULT Invoke0(DISPID dispid, VARIANT* pvarRet = NULL);
HRESULT Invoke0(LPCOLESTR lpszName, VARIANT* pvarRet = NULL);
HRESULT Invoke1(DISPID dispid, VARIANT* pvarParam1, VARIANT* pvarRet = NULL);
HRESULT Invoke1(LPCOLESTR lpszName, VARIANT* pvarParam1, VARIANT* pvarRet = NULL);
HRESULT Invoke2(DISPID dispid, VARIANT* pvarParam1, VARIANT* pvarParam2, VARIANT* pvarRet = NULL);
HRESULT Invoke2(LPCOLESTR lpszName, VARIANT* pvarParam1, VARIANT* pvarParam2, VARIANT* pvarRet = NULL);
HRESULT InvokeN(DISPID dispid, VARIANT* pvarParams, int nParams, VARIANT* pvarRet = NULL);
HRESULT InvokeN(LPCOLESTR lpszName, VARIANT* pvarParams, int nParams, VARIANT* pvarRet = NULL);

I tried the cast. As a matter of fact, I tried these:

cb.Invoke1(L("Test"), var);
cb.Invoke1((LPCOLESTR) L("Test"), var);
cb.Invoke1((LBCOLESTR) m_bstrCallBackPrefix, var);
cb.Invoke1((LBCOLESTR) m_bstrCallBackPrefix.m_str, var);
cb.Invoke1(m_bstrCallBackPrefix.m_str, var);

and some combinations of it. Most of these work with the Invoke0 and InvokeN methods, but none work with the Invoke1 and Invoke2 methods. The compiler keeps complaining about its inability to convert the given parameter type to a long.
Avatar of 12bsure

ASKER

Sorry, typo. Must be: L"Test" instead of L("Test"). Otherwise the compiler will complain.
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 12bsure

ASKER

Thanks, you are completely and totally right! It was just a typo in my code! Leaving the ampersand out gives the error I wrote, so I focused on the first argument. Passing the reference to the variant didn't give an error at all. This also explaines why the other functions Invoke0 and InvokeN worked: they don't need a VARIANT* the way Invoke1 and Invoke2 need one.

I was fooled by the erroneous message of the compiler and didn't see the error. The funny thing is, that once I copied the methods into a new class, inherited from CComDispatchDriver and renaming the methods to InvokeByNameX, I did use the right VARIANT*. (after the compiler issued a more understandable error about it.)

I am very glad it wasn't the compiler or the ATL code this time. Instead, it was just me :-)

Thanks for helping me out. You receive a full A for a full solution albeit a different one than I expected.
Does anyone know if this accepted answer was posted as a comment or as an answer?
nietod:
>>Does anyone know if this accepted answer was posted as a comment or as an answer?

I do, without even checking my records.  

I click Answer in only one situation -- on abandoned questions to which I have expended effort.  I consider a question to be abandoned after the Asker has not responded two two verbal pings, spaced about 5 days apart.  In other words, after the Asker's last response was over 10 days old. I'll wait longer if other experts continue proposing meaningful suggestions.

That was not the situation here -- by several criteria.  Thus there is no possibility that I locked this question. 12bsure clicked the [Accept comment as Answer] as I'm sure he'll verify.

-- Dan
Avatar of 12bsure

ASKER

>>> Does anyone know if this accepted answer was posted as a comment or as an answer?
> as I'm sure he'll verify.
Herewith: he's right, it was a comment.

Btw, Nietod, why did you ask? Did you expect a notification, but didn't you get any? Or did you have a meaningful suggestion? They're of course still welcome.

Btw2, Dan, that's a good policy you have :-)

Regards
Thanks,

I'm trying to measure the number of PAQs closed with comments vs answers.
hi 12bsure,
There has been a discussion thread in which nietod and a few others tried to defend the practice of posting answers to lock questions.  Axter and myself and a few others object to this and would like to see all C++ section experts simply post comments.  Our basic premise is that even if a post answers the question, it may not be a perfect solution for the Asker.  This question is typical.  My 'sidebar' comment -- that actually led you to a solution -- came only after some give-and-take.

Nietod seems to be checking up on me to see if I actually practice what I preach.

-- Dan
Don't take it personally.  I'm not checking up on you.  I posted this comment to the 20 most recent (at the time) PAQs to try to measure how they were "handled".    You can check the other PAQs around this one to see the same comment..  Axter is claiming that in this topic area by far the most questions are handled through comments, not answers.  So far the evidence does not really support that.