Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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中动态创建对象(使用radiohead库)_C_Arduino - Fatal编程技术网

在C中动态创建对象(使用radiohead库)

在C中动态创建对象(使用radiohead库),c,arduino,C,Arduino,我试图使用NRF24l01+在我的arduino上实现动态地址分配,但在我的设备接收到分配的地址后,我无法获得全局初始化的RHReliableDatagram 这就是我所拥有的(由于某些原因,它不起作用: manager = new RHReliableDatagram(driver, ID); 错误: sketch_oct02b.ino: In function 'void setup()': sketch_oct02b:47: error: no match for 'operator='

我试图使用NRF24l01+在我的arduino上实现动态地址分配,但在我的设备接收到分配的地址后,我无法获得全局初始化的RHReliableDatagram

这就是我所拥有的(由于某些原因,它不起作用:

manager = new RHReliableDatagram(driver, ID);
错误:

sketch_oct02b.ino: In function 'void setup()':
sketch_oct02b:47: error: no match for 'operator=' in 'manager = (((RHReliableDatagram*)operator new(267u)), (<anonymous>->RHReliableDatagram::RHReliableDatagram(((RHGenericDriver&)(& driver.RH_NRF24::<anonymous>.RHNRFSPIDriver::<anonymous>)), ((uint8_t)ID)), <anonymous>))'
C:\Users\****\Documents\HAS\HAS-mc\libraries\RadioHead/RHReliableDatagram.h:66: note: candidates are: RHReliableDatagram& RHReliableDatagram::operator=(const RHReliableDatagram&)
我的问题在这里的最小再现:

#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
#include <SPI.h>
#include <DHT.h>

RH_NRF24 driver(8,10);

int ID = 255; //init ID, will be reassigned by server
RHReliableDatagram manager(driver,ID);//255 is the ID before init


void setup() 
{
  ID = 15;
  manager = new RHReliableDatagram(driver, ID);//NOTE added this
  //spi.setPins(13, 4, 3); //miso mosi sck
  if (!manager.init()){
    Serial.println("NRF failed to initialise");
    digitalWrite(PIN_NRF_ERROR,HIGH);
  } else {
    Serial.println("NRF succesfully initialized");
  }
}

void loop()
{

}
#包括
#包括
#包括
#包括
RH_NRF24驱动器(8,10);
int ID=255;//init ID将由服务器重新分配
RHReliableDatagram管理器(驱动程序,ID);//255是初始化之前的ID
无效设置()
{
ID=15;
manager=新的RhrReliableDatagram(驱动程序,ID);//注意添加了这个
//spi.setPins(13,4,3);//味噌莫斯sck
如果(!manager.init()){
Serial.println(“NRF未能初始化”);
数字写入(引脚NRF错误,高);
}否则{
Serial.println(“NRF成功初始化”);
}
}
void循环()
{
}
无线电头库的文档如下所示:

您需要将动态分配的对象分配给该对象类型的指针,例如

type *p_var = new type(initializer)
在代码中,它应该被删除

RHReliableDatagram manager(driver,ID);//255 is the ID before init
改变

manager = new RHReliableDatagram(driver, ID);//NOTE added this


此外,您还需要记住使用
delete()
在不再需要时释放管理器提供的内存。

我迟到了,但是-有一个函数可以做到这一点

使用:

manager = new RHReliableDatagram(driver, ID);//NOTE added this
RHReliableDatagram *manager = new RHReliableDatagram(driver, ID);//NOTE added this
manager.setThisAddress(ID);

void RHDatagram::setThisAddress(uint8_t thisAddress)
{
    _driver.setThisAddress(thisAddress);
    // Use this address in the transmitted FROM header
    setHeaderFrom(thisAddress);
    _thisAddress = thisAddress;
}