尝试编译stir库:错误:从‘;的转换无效;常量字符*’;至‘;字符*’; 首先,我对C++和C很新,所以对我来说很容易: 第二,我知道这个问题以前曾以多种形式被问过很多次,但我能理解 如何将答案与我的案例联系起来
我正试图从STIL库中编译一个名为utilities.cxx的文件,该库具有某种类型的“开源”许可证(不是真正的LGPL,因此我不知道是否可以将它的重要部分放在这里 该代码具有以下功能:尝试编译stir库:错误:从‘;的转换无效;常量字符*’;至‘;字符*’; 首先,我对C++和C很新,所以对我来说很容易: 第二,我知道这个问题以前曾以多种形式被问过很多次,但我能理解 如何将答案与我的案例联系起来,c++,constants,C++,Constants,我正试图从STIL库中编译一个名为utilities.cxx的文件,该库具有某种类型的“开源”许可证(不是真正的LGPL,因此我不知道是否可以将它的重要部分放在这里 该代码具有以下功能: char *replace_extension(char *file_in_directory_name, const char * const extension) { char * location_of_dot = strchr(find_filename(file_in_di
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;
}
编译时会出现以下错误:
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
char * location_of_dot =
strchr(find_filename(file_in_directory_name),'.');
任何帮助都将不胜感激…谢谢
奥兹
好的,第一部分已经回答了…下面是导致第二个错误的函数:
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;
}
此行导致错误:
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
char * location_of_dot =
strchr(find_filename(file_in_directory_name),'.');
使用const char*
作为第一个参数调用时,返回一个const char*
,而不是char*
(我假设find_filename()
返回一个const char*
,否则不会看到此错误)
由于要分配给由strchr
返回的内存位置,因此不希望使用此重载版本。请更改find\u filename()
以返回char*
更新:您已经发布了find_filename()
的代码,更改返回类型将涉及更改其他内容(除此之外没有多大意义)。相反,将find_filename()
的返回值强制转换为char*
或强制转换strchr()的结果
到字符*
示例(使用a):
char*location\u of\u dot=const\u cast(
strchr(查找文件名(文件名在目录中),'.');
添加以下重载:
char* find_filename( char* filename_with_directory)
{
char const* const fname = filename_with_directory;
return const_cast<char*>( find_filename( fname ) );
}
char*find_filename(char*filename_和_目录)
{
char const*const fname=filename\u和目录;
返回常量转换(查找文件名(fname));
}
免责声明:编译器未触及代码
干杯,不符合标准。@Let\u Me\u Be:Check out.当第一个参数是
const char*
而不是char*
正在阅读什么手册时,返回const char*
会过载。man strchr
说char*strchr(const char*s,int c)
Hi Cameron,谢谢大家的快速回复。但是这段代码也没有编译,它引发了另一个错误…g++-O3-ffast math-DNDEBUG-Wall-Wno deprecated-I../lmf_v2.0/includes-D_FILE_OFFSET_BITS=64-I./include-DSTIR_SIMPLE_位图-DSC_XWINDOWS-o opt/buildblock/utilities.o-MMD-MP-c buildblock/utilities.cxx buildblockk/utilities.cxx:infunction'char*stir::replace_extension(char*,const char*):buildblock/utilities.cxx:228:error:assignment只读位置“*location_of_dot”make:**[opt/buildblock/utilities.o]error 1由于*(location_of_dot)的原因,代码无法正常编译='\0';
-您需要更改查找文件名
。