First, I’m pretty new to C++ and C so be easy on me 🙂
Second, I know this question has be asked many times in many forms before, but I could figure
how to bend the answers to my case …
I am trying to compile a file called utilities.cxx from the STIL library which has some kind of and “open source” license (not really LGPL and so I don’t know if I can put here significant parts of it…
The code has the following function in it:
char *replace_extension(char *file_in_directory_name,
const char * const extension)
{
char * location_of_dot =
strchr(find_filename(file_in_directory_name),'.');
// first truncate at extension
if (location_of_dot!= NULL)
*(location_of_dot) = '\0';
strcat (file_in_directory_name,extension);
return file_in_directory_name;
}
Compiling it gives the error:
g++ -O3 -ffast-math -DNDEBUG -Wall -Wno-deprecated -I../lmf_v2.0
/includes -D_FILE_OFFSET_BITS=64 -I./include -DSTIR_SIMPLE_BITMAPS -DSC_XWINDOWS
-o opt/buildblock/utilities.o -MMD -MP -c buildblock/utilities.cxx
buildblock/utilities.cxx: In function ‘char* stir::replace_extension(char*, const
char*)’:
buildblock/utilities.cxx:225: error: invalid conversion from ‘const char*’ to ‘char*’
make: *** [opt/buildblock/utilities.o] Error 1
Any help would be appreciated … Thanks,
Oz
Ok, first part already answered … here is the function which causes the second error:
const char * const
find_filename(const char * const filename_with_directory)
{
const char * name;
#if defined(__OS_VAX__)
name = strrchr(filename_with_directory,']');
if (name==NULL)
name = strrchr(filename_with_directory,':');
#elif defined(__OS_WIN__)
name = strrchr(filename_with_directory,'\\');
if (name==NULL)
name = strrchr(filename_with_directory,'/');
if (name==NULL)
name = strrchr(filename_with_directory,':');
#elif defined(__OS_MAC__)
name = strrchr(filename_with_directory,':');
#else // defined(__OS_UNIX__)
name = strrchr(filename_with_directory,'/');
#endif
if (name!=NULL)
// KT 10/01/2000 name++ changed to name+1
return name+1;
else
return filename_with_directory;
}
This line is causing the error:
strchr()returns aconst char*, not achar*when called with aconst char*as the first argument (I’m assumingfind_filename()returns aconst char *, otherwise you wouldn’t be seeing this error).Since you want to assign to the memory location returned by
strchr, you don’t want to use this overloaded version. Changefind_filename()to return achar*.UPDATE: You’ve since posted the code for
find_filename()and changing the return type would involve changing other stuff (and doesn’t make much sense besides). Instead, cast either the return value offind_filename()to achar*or cast the result ofstrchr()to achar*.Example (uses a const cast):