【问题标题】:C++ how to convert from char * to unsigned char * in function call?C++ 如何在函数调用中从 char * 转换为 unsigned char *?
【发布时间】:2017-07-12 13:27:46
【问题描述】:

在构建我的 C++ 应用程序时,在这行代码处构建失败

if (!PyTuple_GetByte(poArgs, 0, &SourceCell.window_type))

出现这个错误

错误 C2664:“PyTuple_GetByte”:无法将参数 3 从 'char *' 到 'unsigned char *'

这是被调用的函数:

bool PyTuple_GetByte(PyObject* poArgs, int pos, unsigned char* ret);

第三个参数&SourceCell.window_type是类型char

有没有办法像

那样在函数调用中转换/转换参数
if (!PyTuple_GetByte(poArgs, 0, reinterpret_cast<unsigned char*>(&SourceCell.window_type)))

还是我必须以其他方式处理它?

【问题讨论】:

  • 试试吧.....
  • A static_cast 应该可以正常工作 IIRC。
  • if (!PyTuple_GetByte(poArgs, 0, reinterpret_cast&lt;unsigned char*&gt;(&amp;SourceCell.window_type))) 没有用吗?
  • @πάνταῥεῖ 我认为static_cast 在这里不起作用:stackoverflow.com/questions/10151834/… 因为它们是不同的类型,不是吗?
  • 你必须 reinterpret_cast 或 c 风格的演员在这里

标签: c++ casting char unsigned-char


【解决方案1】:

从错误中,PyTuple_GetByte 函数的签名需要unsigned char* 类型的第三个参数,但您在调用时传递了char* 类型的变量。我认为你有两个选择。

  1. 您可以更改函数PyTuple_GetByte 的签名以期待char* 参数。

  2. 您需要将输入变量从char* 类型转换为unsigned char* 类型,然后才能将其传递给PyTuple_GetByte

转换通常是这样的:

unsigned char* convert_var = reinterpret_cast<unsigned char*>(&SourceCell.window_type); // (c++ way)

unsigned char* convert_var = (unsigned char*)(&SourceCell.window_type); // (c way)

【讨论】:

    猜你喜欢
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 2013-07-21
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多