From: Jeov� Almeida on
Hello,

I'm (trying to) implementing an intenet helper static class for GET and POST
calls:

CString result = CHttpClient::Get("www.servername.com")

The problem is I'm using wininet and when I call
CInternetSession::GetHttpConnection(szServerUrl), the compiler gives me the
error

C2352: 'CInternetSession::GetHttpConnection' : illegal call of non-static
member

How can I achieve the same result (that is call CHttpClient::Get()
statically) using wininet, bypassing this error

As an additional info, the member declaration in the .h file is
static CString Get(LPCTSTR strURL, CString& strErrorMsg=CString() );

Thanks.


From: Doug Harrison [MVP] on
On Wed, 7 May 2008 17:36:55 -0400, "Jeov� Almeida" <jeovaalmeida(a)yahoo.com>
wrote:

>Hello,
>
>I'm (trying to) implementing an intenet helper static class for GET and POST
>calls:

Using a namespace instead of a class may be a better bet.

>CString result = CHttpClient::Get("www.servername.com")
>
>The problem is I'm using wininet and when I call
>CInternetSession::GetHttpConnection(szServerUrl), the compiler gives me the
>error
>
>C2352: 'CInternetSession::GetHttpConnection' : illegal call of non-static
>member
>
>How can I achieve the same result (that is call CHttpClient::Get()
>statically) using wininet, bypassing this error

To call a non-static member function, you need an instance of the class, so
you'll have to create a CInternetSession object.

>As an additional info, the member declaration in the .h file is
>static CString Get(LPCTSTR strURL, CString& strErrorMsg=CString() );

This seems familiar, and if so, it's not what I showed you. The second
parameter must be a const reference or simply a CString, because it's
illegal to bind a temporary to a non-const reference. (Yes, VC still allows
it for compatibility reasons.) Also, it's more efficient to implement this
as two functions, because then you don't construct and destroy a temporary
CString at each call site.

--
Doug Harrison
Visual C++ MVP
From: Jeov� Almeida on
Hello Doug,

You said
> The second parameter must be a const reference or simply a CString,
> because it's illegal to bind a temporary to a non-const reference.

The second parameter is an output parameter, that is, if some error occurs
during the processing I can get the error message and log it, if I want. If
I make it a const reference, when I assign a value the strErrorMsg, I get
the error:

C2678: binary '=' : no operator found which takes a left-hand operand of
type 'const CString' (or there is no acceptable

The idea of implementing it with two functions is good, and I'll stick to it
when I correct the errors I'm getting.

> To call a non-static member function, you need an instance of the class,
> so
> you'll have to create a CInternetSession object.

I did this and it worked perfectly. Thank u.

Jeov�

"Doug Harrison [MVP]" <dsh(a)mvps.org> escreveu na mensagem
news:sd9424dcihq4ctsldbd00ippf6odihsr74(a)4ax.com...
> On Wed, 7 May 2008 17:36:55 -0400, "Jeov� Almeida"
> <jeovaalmeida(a)yahoo.com>
> wrote:
>
>>Hello,
>>
>>I'm (trying to) implementing an intenet helper static class for GET and
>>POST
>>calls:
>
> Using a namespace instead of a class may be a better bet.
>
>>CString result = CHttpClient::Get("www.servername.com")
>>
>>The problem is I'm using wininet and when I call
>>CInternetSession::GetHttpConnection(szServerUrl), the compiler gives me
>>the
>>error
>>
>>C2352: 'CInternetSession::GetHttpConnection' : illegal call of non-static
>>member
>>
>>How can I achieve the same result (that is call CHttpClient::Get()
>>statically) using wininet, bypassing this error
>
> To call a non-static member function, you need an instance of the class,
> so
> you'll have to create a CInternetSession object.
>
>>As an additional info, the member declaration in the .h file is
>>static CString Get(LPCTSTR strURL, CString& strErrorMsg=CString() );
>
> This seems familiar, and if so, it's not what I showed you. The second
> parameter must be a const reference or simply a CString, because it's
> illegal to bind a temporary to a non-const reference. (Yes, VC still
> allows
> it for compatibility reasons.) Also, it's more efficient to implement this
> as two functions, because then you don't construct and destroy a temporary
> CString at each call site.
>
> --
> Doug Harrison
> Visual C++ MVP


From: Doug Harrison [MVP] on
On Wed, 7 May 2008 19:56:29 -0400, "Jeov� Almeida" <jeovaalmeida(a)yahoo.com>
wrote:

>Hello Doug,
>
>You said
>> The second parameter must be a const reference or simply a CString,
>> because it's illegal to bind a temporary to a non-const reference.
>
>The second parameter is an output parameter, that is, if some error occurs
>during the processing I can get the error message and log it, if I want. If
>I make it a const reference, when I assign a value the strErrorMsg, I get
>the error:
>
>C2678: binary '=' : no operator found which takes a left-hand operand of
>type 'const CString' (or there is no acceptable

Right, you can't assign to an object through a const reference, unless the
class is very, very weird and defines a const operator=.

I didn't realize it was an out parameter. What I said about binding a
temporary to a non-const reference is correct, and you shouldn't use a
default argument here. (Please ignore the fact that it works. <g> Try
compiling with /Za and you'll find that it doesn't.) It would be better to
stop using the default argument and split the function into two functions,
e.g.

static CString Get(LPCTSTR strURL)
{
CString x;
return Get(strURL, x);
}

static CString Get(LPCTSTR strURL, CString& strErrorMsg);

How do you distinguish an error return from a non-error return? If you
throw an exception, you can include an error message in the exception
object, and then you won't need the strErrorMsg parameter.

--
Doug Harrison
Visual C++ MVP
 | 
Pages: 1
Prev: Removing a CDockable Pane
Next: CodeGear is sold