C++ const到const char*的转换无效,如何修复?使用gcc7失败
这可以使用较旧版本的gcc进行编译,但在使用C++17的gcc 7时失败。 这是我得到的错误: 错误:从“char”到“const char*”[-fppermissive]end\u of_line='\0'的转换无效 似乎无法解决此编译错误 代码如下:C++ const到const char*的转换无效,如何修复?使用gcc7失败,c++,gcc,c++17,C++,Gcc,C++17,这可以使用较旧版本的gcc进行编译,但在使用C++17的gcc 7时失败。 这是我得到的错误: 错误:从“char”到“const char*”[-fppermissive]end\u of_line='\0'的转换无效 似乎无法解决此编译错误 代码如下: /*! * \brief Find the beginning of the next line in the given buffer. * * \param[in] str buffer to search for the begi
/*!
* \brief Find the beginning of the next line in the given buffer.
*
* \param[in] str buffer to search for the beginning of the next line
* \param[inout] ctx
* \parblock
* pointer to the end of the line (saved by this method)
*
* This pointer must be valid, and it must be set to NULL by the caller the first time.
* \endparblock
*
* \return a pointer to the first character in the next line, or NULL if we have already hit EOF
*/
const char* Foo::GetNextLine(const char* str, const char** ctx)
{
if(str == NULL)
{
return NULL;
}
// Save a pointer to the end of the line (first time, it should be set to NULL by the caller).
const char*& end_of_line = *ctx;
if(end_of_line && *end_of_line == '\0')
{
end_of_line = '\0';
return NULL;
}
// The start of this line is one character after the end of the last one.
const char* start_of_line = end_of_line ? end_of_line + 1 : str;
// Are we at the end of the whole thing?
if(*start_of_line == '\0')
{
end_of_line = '\0'; // Reset the context state to get ready for the next load!
return NULL;
}
// Read forward to the end of the line.
end_of_line = start_of_line;
while(*end_of_line != '\n')
{
if(*end_of_line == '\0')
{
break;
}
++end_of_line;
}
return start_of_line;
}
由于C++11已被缺陷报告修改,因此不能将任意零值表达式指定给指针。它必须是常量0或类型为nullptr_t的对象,即nullptr。这可以隐藏在宏NULL后面 _行的表达式end_='\0';尝试将字符常量指定给指针。这是不允许的。假设您的目的是清空原始指针,那么应该将该行更改为read
end_of_line = nullptr;
你能发布命令和gcc调用的输出吗?