我正在开发一个c++应用程序,用一些信息填充蓝牙数组。它是基于混合平台BLE_API,但我不认为这是相关的。我有下面的代码,我正在尝试将其重新分解为一个函数。
GattAttribute nameDescr1(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"Percentage", strlen("Percentage"));
GattAttribute *pdescriptors[] = { &nameDescr1 };
PercentageFill(PercentageUUID, valueBytes.getPointer(),
valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
pdescriptors,
sizeof(pdescriptors) / sizeof(GattAttribute*)),到目前为止我已经知道了:
GattAttribute produceName (char title[]) {
GattAttribute nameDescr(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)title, strlen(title));
GattAttribute *descriptors[] = { &nameDescr };
return descriptors;
}但是,不出所料,我抛出了一个错误:
错误:没有合适的构造函数将"GattAttribute *1“转换为"GattAttribute”
我可以理解为什么要抛出这个数组,但不确定应该如何返回整个数组,因为这是"PercentageFill“构造函数所需的格式。
谢谢。
更新:
为了给出完整的上下文,下面是我正在设置的其他Characteristcis (每个字符都有不同的名称):
NewService(BLE &_ble, uint8_t percentageFill, uint8_t replacementDue) :
ble(_ble),
valueBytes(percentageFill),
PercentageFill(PercentageUUID, valueBytes.getPointer(),
valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
pdescriptors,
sizeof(pdescriptors) / sizeof(GattAttribute*)),
Time( TimeUUID,
&replacementDue,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
tdescriptors,
sizeof(tdescriptors) / sizeof(GattAttribute*)),
UseProfile( UseProfileUUID,
&controlPointValue,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
Udescriptors,
sizeof(Udescriptors) / sizeof(GattAttribute*)),) {
setupService();
}发布于 2015-11-18 15:32:59
函数是produceName,它声明返回一个GattAttribute对象,但您正在尝试返回指向GattAttribute对象的指针数组。差别很大。
但这并不是你代码中最糟糕的部分。如果您修复了声明返回类型,以便代码生成,那么您将面临一个更糟糕的问题,这将导致未定义的行为:返回指向局部变量的指针。一旦函数返回,这些局部变量将停止存在,并且不能使用指向它们的任何指针。
发布于 2015-11-18 16:06:00
首先:请注意,原始代码中的pdescriptors数组只有一个元素长。因此,指向对象的直接指针将工作得很好,或者,如果填充百分比不需要一个指针数组,它就会工作得很好。我们可以通过传递一个指针到指针的大小来模拟这一点。请注意:原始代码中的sizeof(...)/sizeof(...)计算也意味着返回1,当您引入函数边界时(特别是当您将数组作为参数传递给函数时),这会变得很棘手。
除此之外,您的问题还有点不清楚:您是否打算实现不同的GattAttribute值?如果没有,你很可能会做这样的事情:
void updatePercentage(WhateverTypeValueBytesIs valueBytes) {
GattAttribute nameDescr(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"Percentage", strlen("Percentage"));
GattAttribute * ptr = &nameDescr; // needed, because we want to pass pointer-to-pointer-to-nameDescr
PercentageFill(PercentageUUID, valueBytes.getPointer(),
valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
&ptr, 1), /* are we sure about trailing comma here? */
// other relevant trailing code?
}从注释中提到的不可访问的复制构造函数的错误消息判断,GattAttribute可能是一个常规的构造函数,因此不需要在那里创建额外的函数。如果您想要将这个特定的GattAttribute转换成您可以隐藏在函数接口后面的东西,并在需要时进行“查找”,那么您可以将它变成这样的单例(例如,实现相同目标的其他方法存在):
GattAttribute * getNameDescriptor(void) {
static GattAttribute nameDescr(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"Percentage", strlen("Percentage"));
return &nameDescr;
}然后,您可以像这样使用该函数:
void updatePercentage(WhateverTypeValueBytesIs valueBytes) {
GattAttribute * ptr = getNameDescriptor(); // needed, because we want to pass pointer-to-pointer-to-nameDescr
PercentageFill(PercentageUUID, valueBytes.getPointer(),
valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
&ptr, 1), /* are we sure about trailing comma here? */
// other relevant trailing code?
}编辑以根据注释添加其他选项:
void updatePercentage(WhateverTypeValueBytesIs valueBytes, const char* name) {
GattAttribute nameDescr(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (const uint8_t *) name, strlen(name));
GattAttribute * ptr = &nameDescr; // needed, because we want to pass pointer-to-pointer-to-nameDescr
PercentageFill(PercentageUUID, valueBytes.getPointer(),
valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
&ptr, 1), /* are we sure about trailing comma here? */
// other relevant trailing code?
}或者另一个选择:琐碎,通过引用传递一个完全初始化的GattAttribute:
void updatePercentage(WhateverTypeValueBytesIs valueBytes, GattAttribute & descr) {
GattAttribute * ptr = &descr; // needed, because we want to pass pointer-to-pointer-to-descr
PercentageFill(PercentageUUID, valueBytes.getPointer(),
valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
&ptr, 1), /* are we sure about trailing comma here? */
// other relevant trailing code?
}你这样称呼它:
void foo(WhateverTypeValueBytesIs valueBytes) {
GattAttribute nameDescr(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"Percentage", strlen("Percentage"));
updatePercentage(valueBytes, nameDescr);
}显然,与使用引用不同,您还可以重新处理函数以获取指向GattAttribute对象的指针(并使用该指针代替ptr变量,与示例中使用ptr变量的方式相同)。
进一步添加:请注意,当您将valueBytes传递给updatePercentage时,您可能希望避免复制它,在这里,您可能希望通过引用传递它。
https://stackoverflow.com/questions/33784024
复制相似问题