Can't decipher compiler error

G

George Bell

The compiler(gcc) gives me this error

faxGettyApp.c++:596: ANSI C++ forbids implicit conversion from `void *' in
assignment
make[3]: *** [faxGettyApp.o] Error 1

on this code

void
faxGettyApp::setEnvVar(char* var, char* val)
{
char * v;
596 v = malloc( strlen(var) + strlen(val) + 2 );
if ( v == NULL )
traceServer("runGetty: setEnvVar: cannot malloc" );
else
{
sprintf( v, "%s=%s", var, val );
if ( putenv( v ) != 0 )
traceServer("runGetty: putenv: putenv failed" );
}
}


this function is called by:

setenv("ABCDEF", abcd)

where "abcd" is of type fxStr
where fxStr is special class for strings defined in the program.

What does this error mean?
Thanks

George
 
J

John Harrison

George Bell said:
The compiler(gcc) gives me this error

faxGettyApp.c++:596: ANSI C++ forbids implicit conversion from `void *' in
assignment
make[3]: *** [faxGettyApp.o] Error 1

on this code

void
faxGettyApp::setEnvVar(char* var, char* val)
{
char * v;
596 v = malloc( strlen(var) + strlen(val) + 2 );
if ( v == NULL )
traceServer("runGetty: setEnvVar: cannot malloc" );
else
{
sprintf( v, "%s=%s", var, val );
if ( putenv( v ) != 0 )
traceServer("runGetty: putenv: putenv failed" );
}
}


this function is called by:

setenv("ABCDEF", abcd)

where "abcd" is of type fxStr
where fxStr is special class for strings defined in the program.

What does this error mean?
Thanks

George

Guess what, it means you cannot implicitly convert from void* (to char*).

char * v;
v = malloc( strlen(var) + strlen(val) + 2 );

v is type char*, malloc returns type void*. In C++ it is not legal to
convert from void* to char*. It is legal in C, maybe this is what is
confusing you, the code you've posted looks like C code not C++ code.
Perhaps you should try a C compiler instead.

If you want to carry on with a C++ compiler then you are going to have to
add a cast

v = (char*)malloc( strlen(var) + strlen(val) + 2 );

john
 
A

Andrey Tarasevich

George said:
The compiler(gcc) gives me this error

faxGettyApp.c++:596: ANSI C++ forbids implicit conversion from `void *' in
assignment
make[3]: *** [faxGettyApp.o] Error 1

There is no implicit conversion from 'void*' to 'char*' in C++. If your
code depends on such conversion, you have to do it explicitly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top