从 'const char*' 型到'__FlashStringHelper*'型的reinterpret_cast会丢弃限定符

reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away qualifiers

本文关键字:cast FlashStringHelper char const 型到 型的 reinterpret      更新时间:2023-10-16

我想在AVR Studio中使用Adafruit_CC3000 arduino库。我已经按照这个说明将Adafruit arduino lib与AVR工作室一起使用,所以我也可以使用其他AVR功能。但是我在编译代码时遇到相同的错误 50 次。

错误 5 从类型"const char*"reinterpret_cast为类型 "__FlashStringHelper*"被抛弃 限定符 E:\arduino-1.0.1\libraries\Adafruit_CC3000\Adafruit_CC3000.cpp 183 3 ATmega32_WSClient_CC3K

我在网上搜索过这种错误。 但我未能理解这个问题。我要求让我了解此代码中的哪件事会产生此错误?

reinterpret_cast可以在不相关的指针类型之间进行转换,但不能删除constvolatile限定符。为此,您需要const_cast

选项是(大致按增加的顺序):

  • 首先不要使用错误的指针类型;
  • 投射到const __FlashStringHelper*,如果你不需要修改对象;
  • char*转换,如果你确实需要修改它;
  • 如果您坚持完全放弃类型系统,请使用reinterpret_cast<__FlashStringHelper*>(const_cast<char*>(whatever))或蛮力(__FlashStringHelper*)whatever