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

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;
}

Answers


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),'.'));

Need Your Help

Purpose of floating points in Ruby

ruby floating-point

What is the purpose of a floating number in Ruby? I found some information about using less bytes or increasing accuracy, but I do not understand why you would not always use floats. Wouldn't it gi...

how to wait for batch command to complete its excecution in java

java process batch-processing runtime.exec

I want to wait till my batch command completes its execution and creates a csv file as out put.the code is as bellow for excecuting batch file

How do I set encoding in an NpgsqlConnection

c# postgresql encoding npgsql

I have a PostgreSQL database, which uses character encoding WIN1252.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.