g++ (MinGW) 无法使用选项 -fshort-wchar 将 wchar_t* 转换为 unsigned Short*

发布于 2025-01-14 06:52:16 字数 664 浏览 2 评论 0原文

我正在将 C++ 项目从 VisalStudio 移植到 GCC/MinGw。

原始项目使用编译器标志/Zc:wchar_t-,它将类型wchar_t定义为无符号短整型(应为2字节)。 现在,当我使用 g++ 和选项 -fshort-wchar 进行编译时。根据文档,此选项还应将 wchar_t 定义为无符号短。

现在有一个

void myfuncExample(short unsigned int*)

使用 wchar_t* 变量调用的函数,该变量应该可以转换为无符号的 Short*。

但是,编译时出现此错误:

error: invalid conversion from 'wchar_t*' to 'short unsigned int*' [-fpermissive]
myfuncExample(buf);
              ^~~

为什么不能转换?如果我删除 -fshort-wchar 选项,错误是相同的。看起来 g++ 忽略了这个选项。有什么建议吗?

I am porting a C++ Project from VisalStudio to GCC/MinGw.

The original project uses the compiler flag /Zc:wchar_t-, which defines the type wchar_t as an unsigned short (should be 2 bytes).
Now when I compile with g++ and the option -fshort-wchar. According to the documentation this option should also define wchar_t as an unsigned short.

Now there is a function

void myfuncExample(short unsigned int*)

which is called with an wchar_t* variable, which should be castable to an unsigned short*.

However, when compiling I get this error:

error: invalid conversion from 'wchar_t*' to 'short unsigned int*' [-fpermissive]
myfuncExample(buf);
              ^~~

Why can it no be converted? If i remove the -fshort-wchar option, the error is the same. It seems like g++ is ignoring the option. Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏天碎花小短裙 2025-01-21 06:52:16

-fshort-wcharsizeof(wchar_t) 设置为 2(这已经是 Windows 上的默认值,与其他地方的 4 不同)。

它不会使 wchar_tunsigned Short 具有相同的类型(即使它们具有相同的大小和符号)。

您需要修复代码。

-fshort-wchar sets sizeof(wchar_t) to 2 (which is already the default on Windows, unlike 4 elsewhere).

It doesn't make wchar_t the same type as unsigned short (even though they will have the same size and signedness).

You'll need to fix the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文