从"常量字符*"到"字符*"[-允许]的转换无效;VTK-7.1.1 编译错误



正如标题所说,我不是c ++程序员,我只是想编译它,以便我可以为FreeCAD编译一些我想要的模块。 我在 VTK-8.90 上遇到了问题,我无法弄清楚,所以我正在尝试这个版本,因为它是模块的推荐稳定版本。不是程序员并在谷歌上搜索这个错误,我玩了很多删除和添加/删除各种"const"和"char*"到指示的错误 - 但我真的不知道我在做什么,所以我想我会尝试得到一个具体的答案。(我不认为编写的源代码一定是问题所在。 我正在编译以将其与python3.7一起使用,默认值为2.7,但这应该无关紧要,因为这是c ++代码?

这是具体的错误:

[ 98%] Building CXX object Wrapping/PythonCore/CMakeFiles/vtkWrappingPythonCore.dir/vtkPythonArgs.cxx.o
/src/VTK-7.1.1/Wrapping/PythonCore/vtkPythonArgs.cxx: In instantiation of ‘bool vtkPythonGetStringValue(PyObject*, T*&, const char*) [with T = char; PyObject = _object]’:
/src/VTK-7.1.1/Wrapping/PythonCore/vtkPythonArgs.cxx:287:66:   required from here
/src/VTK-7.1.1/Wrapping/PythonCore/vtkPythonArgs.cxx:105:25: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
a = PyUnicode_AsUTF8(o);
~~~~~~~~~~~~~~~~^~~
make[2]: *** [Wrapping/PythonCore/CMakeFiles/vtkWrappingPythonCore.dir/build.make:63: Wrapping/PythonCore/CMakeFiles/vtkWrappingPythonCore.dir/vtkPythonArgs.cxx.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:9917: Wrapping/PythonCore/CMakeFiles/vtkWrappingPythonCore.dir/all] Error 2
make: *** [Makefile:130: all] Error 2

这是vtkPythonArgs.cxx的第一个引用部分(从第93行开始):

template <class T> inline
bool vtkPythonGetStringValue(PyObject *o, T *&a, const char *exctext)
{
if (PyBytes_Check(o))
{
a = PyBytes_AS_STRING(o);
return true;
}
#ifdef Py_USING_UNICODE
else if (PyUnicode_Check(o))
{
#if PY_VERSION_HEX >= 0x03030000
a = PyUnicode_AsUTF8(o);
return true;
#else
PyObject *s = _PyUnicode_AsDefaultEncodedString(o, NULL);
if (s)
{
a = PyBytes_AS_STRING(s);
return true;
}
exctext = "(unicode conversion error)";
#endif
}
#endif
PyErr_SetString(PyExc_TypeError, exctext);
return false;
}
inline bool vtkPythonGetStdStringValue(PyObject *o, std::string &a, const char *exctext)
{
if (PyBytes_Check(o))
{
char* val;
Py_ssize_t len;
PyBytes_AsStringAndSize(o, &val, &len);
a = std::string(val, len);
return true;
}
#ifdef Py_USING_UNICODE
else if (PyUnicode_Check(o))
{
#if PY_VERSION_HEX >= 0x03030000
Py_ssize_t len;
const char* val = PyUnicode_AsUTF8AndSize(o, &len);
a = std::string(val, len);
return true;
#else
PyObject *s = _PyUnicode_AsDefaultEncodedString(o, NULL);
if (s)
{
char* val;
Py_ssize_t len;
PyBytes_AsStringAndSize(s, &val, &len);
a = std::string(val, len);
return true;
}
exctext = "(unicode conversion error)";
#endif
}
#endif
PyErr_SetString(PyExc_TypeError, exctext);
return false;
}

287 行(从 281 开始):

inline
bool vtkPythonGetValue(PyObject *o, char *&a)
{
a = NULL;
return (o == Py_None ||
vtkPythonGetStringValue(o, a, "string or None required"));
}

文件是 1600 行,我不确定整个事情是否会更有帮助。

谢谢。

好吧,看起来函数PyUnicode_AsUTF8必须返回一个char const*。该函数vtkPythonGetValue调用模板函数vtkPythonGetStringValuevtkPythonGetValue接受参数a,一个对非常char量指针的左值引用,并将其传递给vtkPythonGetStringValue,导致它实例化T推导为char而不是char const。指向 const 对象的指针不能分配给指向非常量对象的指针,只允许相反。因此,函数vtkPythonGetValue实际上应该对指向常量char的非常量指针进行左值引用。这样:

inline
bool vtkPythonGetValue(PyObject* o, char const*& a)
{
a = NULL;
return (o == Py_None ||
vtkPythonGetStringValue(o, a, "string or None required"));
}

最新更新