RE: [Dev-C++] ANSI C++ forbids implicit conversion from `void *' in argument passing
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ya...> - 2002-02-10 22:56:42
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Flavio de Souza
> Sent: Sunday, February 10, 2002 8:41 PM
> To: Dev-cpp
> Subject: [Dev-C++] ANSI C++ forbids implicit conversion from
> `void *' in argument passing
>
>
>
> I am trying to compile a windows program and I am getting the
> following compiler error:
>
> ANSI C++ forbids implicit conversion from `void *' in
> argument passing
>
> This error occurs two times in the following piece of code
> where I have the value "0" (see below).
>
> void WinClass::SetResIcons (int resId)
> {
> _class.hIcon = reinterpret_cast<HICON> (
> ::LoadImage (
> _class.hInstance,
> MAKEINTRESOURCE (resId),
> IMAGE_ICON,
> ::GetSystemMetrics (SM_CXICON),
> ::GetSystemMetrics (SM_CYICON),
> 0));
> <-------------------------------------------------------------
> -----------------where i got the error
> // Small icon can be loaded from the same resource
> _class.hIconSm = reinterpret_cast<HICON> (
> ::LoadImage (
> _class.hInstance,
> MAKEINTRESOURCE (resId),
> IMAGE_ICON,
> ::GetSystemMetrics (SM_CXSMICON),
> ::GetSystemMetrics (SM_CYSMICON),
> 0));
> <-------------------------------------------------------------
> -----------------where i got the error
>
> }
>
> Does anyone know a possible cause? What should I do to
> compile it correctly?
>
> Please, help me.
In C++ a void * can not be assigned directly in any pointer type without
casting, e.g. the result of malloc() needs casting in C++. void * is
used a lot in C to emulate generic programming with poor abilities in
the particular area, that is to write functions that do things on
various types of variables using ancient non type safe techniques. C++
has built in support for generic programming with the best possible way,
templates. Templates arre both time efficient, space efficient if you
use specializations where is needed and type-safe at the same time. E.g.
consider the following C++ C-style code:
void *add(void *p1, void *p2, const char *type)
{
if(strcmp(type, "int")
{
if(int *resul=(char *)malloc(sizeof(int)))
{
*result=(int *)p1+(int *)p2;
return result;
}
else if(strcmp(type, "float")
// Pain goes on...
}
Consider the template alternative:
template <class T>
inline T add(T a, T b)
{
return a+b;
}
It is both type safe and maximally efficient.
So better use templates in the place where you use void * for generic
programming.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.d2g.com
* Alternative URL: http://run.to/noicys
|