开发者

trying to compile stir library: error: invalid conversion from ‘const char*’ to ‘char*’

开发者 https://www.devze.com 2023-01-28 13:30 出处:网络
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

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__)
nam开发者_StackOverflowe = 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:

char * location_of_dot = 
    strchr(find_filename(file_in_directory_name),'.');

strchr() returns a const char*, not a char* when called with a const char* as the first argument (I'm assuming find_filename() returns a const 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. Change find_filename() to return a char*.

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 of find_filename() to a char* or cast the result of strchr() to a char*.

Example (uses a const cast):

char * location_of_dot = const_cast<char*>(
    strchr(find_filename(file_in_directory_name),'.'));


add the following overload:

char* find_filename( char* filename_with_directory)
{
    char const* const fname = filename_with_directory;
    return const_cast<char*>( find_filename( fname ) );
}

disclaimer: code untouched by compiler's hands

cheers & hth.,

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号