gpt4 book ai didi

c++ - 如何解决 C++ 中的 "cppcoreguidelines-pro-type-cstyle-cast"错误?

转载 作者:行者123 更新时间:2023-11-28 01:16:01 32 4
gpt4 key购买 nike

我正在使用 Visual Studio 2015 64 位和 Clang 作为我的编译器来开发 C++。

我尝试使用以下代码将 string 转换为 unsigned char*:

string content = "some content from file";
unsigned char* m_Test = (unsigned char*)content.c_str();

但是,当我尝试构建项目时,这导致了错误:

error: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast,-warnings-as-errors]

知道如何解决这个问题吗?如果您能提供一些帮助,我将不胜感激。

最佳答案

您有两个(或 3 个)选项:

1) 将 C 风格的转换替换为适当的 C++ 转换(static_castconst_castreinterpret_castdynamic_cast).

2)(更好的选择)首先找到一种不需要转换的方式来编写代码。

3) 忽略/抑制警告(不是推荐的,尽管它是一个选项)。

关于c++ - 如何解决 C++ 中的 "cppcoreguidelines-pro-type-cstyle-cast"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58694279/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com