Solved

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

Posted on 2001-06-22
11
879 Views
Last Modified: 2010-05-18
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.
0
Comment
Question by:12bsure
  • 4
  • 4
  • 3
11 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 6220440
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
0
 

Author Comment

by:12bsure
ID: 6221216
//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.
0
 

Author Comment

by:12bsure
ID: 6221218
Sorry, typo. Must be: L"Test" instead of L("Test"). Otherwise the compiler will complain.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 49

Accepted Solution

by:
DanRollins earned 100 total points
ID: 6221652
I can't understand it and the bowels of ATL are filled with what one might expect to find in bowels.

This seems to work:

CComVariant varStr("this is varStr");
CComVariant varInt(1);

LPCOLESTR ps1= L"This is ps1";
LPCOLESTR ps2= (LPCOLESTR)varStr.pbstrVal;

cb.Invoke1(ps1, &varInt);
cb.Invoke1(ps2, &varInt);

I verified that these end up at the implementation of

  HRESULT Invoke1(LPCOLESTR lpszName, VARIANT* pvarParam1, VARIANT* pvarRet = NULL);

-==-=-=-=-=-=-=-=-=-=-=-=-
Also, I see in your tests that you are passing var, but the prototype calls for VARIANT* ;i,e, &var

-- Dan
0
 

Author Comment

by:12bsure
ID: 6222656
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6222822
Does anyone know if this accepted answer was posted as a comment or as an answer?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6223368
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
0
 

Author Comment

by:12bsure
ID: 6224010
>>> 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
0
 
LVL 22

Expert Comment

by:nietod
ID: 6224212
Thanks,

I'm trying to measure the number of PAQs closed with comments vs answers.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6225337
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
0
 
LVL 22

Expert Comment

by:nietod
ID: 6225374
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.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

815 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

9 Experts available now in Live!

Get 1:1 Help Now