Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Serial.println中的VSCode ESP32类型错误_C++_Visual Studio Code_Arduino_Esp32 - Fatal编程技术网

C++ Serial.println中的VSCode ESP32类型错误

C++ Serial.println中的VSCode ESP32类型错误,c++,visual-studio-code,arduino,esp32,C++,Visual Studio Code,Arduino,Esp32,我决定为我的ESP32从ArduinoIDE切换到VSCode和平台。我使用带有回调的BLE服务器示例作为测试: #include <Arduino.h> #include <BLEDevice.h> #include <BLEUtils.h> #include <BLEServer.h> #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" #

我决定为我的ESP32从ArduinoIDE切换到VSCode和平台。我使用带有回调的BLE服务器示例作为测试:

#include <Arduino.h>

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

class MyCallBack: public BLECharacteristicCallbacks {

    void onRead(BLECharacteristic *pCharacteristic) {
      std::__cxx11::string val = pCharacteristic->getValue();
      if ((val.length() > 0)) {
        Serial.println(val);
      }
    }
    
};
    

void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE work!");

  BLEDevice::init("Long name works now");
  BLEServer *pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(SERVICE_UUID);
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ
                                       );

  pCharacteristic->setValue("Hello World!");
  pCharacteristic->setCallbacks(new MyCallBack());
  pService->start();
  // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();
  Serial.println("Characteristic defined! Now you can read it in your phone!");
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
}
除了
BLECharacteristic
getValue
之外,返回
std::\uu cxx11::string

我还尝试了
#define_GLIBCXX_USE_cx11_ABI 0
,根据:但我只是用
std::string
得到了相同的错误


如果
getValue
返回正确的类型,为什么我无法将值打印到序列号?

您看到的错误是编译器无法找到
hardwareseerial
println
方法的版本,该方法将
std:\ucxx11::string
作为参数。问题不在于
getValue()
方法返回了错误的东西;问题在于,
Serial.println()
不是为了接受
getValue()
作为参数返回的内容而编写的

您需要强制转换或以其他方式转换蓝牙功能正在返回的
字符串
字符*
,这两种方法都是
println()
方法接受的。您可以使用
.c_str()
方法来执行此操作

      if ((val.length() > 0)) {
        Serial.println(val.c_str());
      }

您可以通过阅读
Serial.println
std::uu cxx11::string

上的文档来找到这一点。我想我把错误解释错了。当它谈到参数类型时,我认为这是它想要的类型,但实际上这就是我给它的类型。使用
c_str
方法有效-谢谢!
      if ((val.length() > 0)) {
        Serial.println(val.c_str());
      }