gpt4 book ai didi

c++ - 错误 : non-static reference member, 无法使用默认赋值运算符

转载 作者:太空狗 更新时间:2023-10-29 20:44:54 25 4
gpt4 key购买 nike

我正在调整一个现有库(“Webduino”,Arduino 的 Web 服务器)以与另一个现有库(“WiFly”,一个 wifi 模块)一起工作,但遇到了问题。每个库都可以独立运行。 Webduino 库希望通过 SPI 使用以太网硬件模块,而 WiFi 模块使用串行端口 (UART)。我得到的错误是:

WiFlyClient.h: In member function 'WiFlyClient& WiFlyClient::operator=(const WiFlyClient&)':
WiFlyClient.h:14:
error: non-static reference member 'WiFlyDevice& WiFlyClient::_WiFly', can't use default assignment operator
WiFlyWebServer.h: In member function 'void WebServer::processConnection(char*, int*)':
WiFlyWebServer.h:492: note: synthesized method 'WiFlyClient& WiFlyClient::operator=(const WiFlyClient&)' first required here

这里是相关的代码片段。请注意,到目前为止,我只修改了 WiFlyWebServer.h (Webduino):

// WiFlyWebServer.h (Webduino)
...
WiFlyServer m_server; // formerly EthernetServer and
WiFlyClient m_client; // EthernetClient
...
void WebServer::processConnection(char *buff, int *bufflen){
...
// line 492
m_client = m_server.available();
...
}



// WiFlyClient.h
class WiFlyClient : public Client {
public:
WiFlyClient();
...
private:
WiFlyDevice& _WiFly;
...
}



// WiFlyClient.cpp
#include "WiFly.h"
#include "WiFlyClient.h"

WiFlyClient::WiFlyClient() :
_WiFly (WiFly) { // sets _wiFly to WiFly, which is an extern for WiFlyDevice (WiFly.h)
...
}


// WiFly.h
#include "WiFlyDevice.h"
...
extern WiFlyDevice WiFly;
...



// WiFlyDevice.h
class WiFlyDevice {
public:
WiFlyDevice(SpiUartDevice& theUart);
...



// WiFlyDevice.cpp
WiFlyDevice::WiFlyDevice(SpiUartDevice& theUart) : SPIuart (theUart) {
/*

Note: Supplied UART should/need not have been initialised first.

*/
...
}

问题源于 m_client = m_server.available();,如果我注释掉问题就消失了(但整个事情都依赖于那条线)。实际问题似乎是 _WiFly 成员在分配 WiFiClient 对象时无法初始化(覆盖?),但我不明白为什么它在没有修改的情况下工作时在这里不起作用。

(是的,我知道头文件里有实现,我不知道他们为什么这么写,不要怪我!)

有什么见解吗?

最佳答案

WiFlyClientWiFly 成员使该类不可分配。原因是赋值不能用于更改引用所指的对象。例如:

int a = 1;
int b = 2;
int &ar = a;
int &br = b;
ar = br; // changes a's value to 2, does not change ar to reference b

由于您所有的 WiFlyClient 都引用相同的 WiFlyDevice 实例,您可以更改 WiFlyClient,因为编译器建议使用静态成员:

// WiFlyClient.h
class WiFlyClient : public Client {
public:
WiFlyClient();
...
private:
static WiFlyDevice& _WiFly;
...
};

然后,您不在构造函数中初始化它,而是在定义它的源文件中进行初始化。

WiFlyDevice & WiFlyClient::_WiFly = WiFly;

关于c++ - 错误 : non-static reference member, 无法使用默认赋值运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12012277/

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