错误:非静态引用成员,不能使用默认赋值运算符

T3d*_*b0t 3 c++ arduino

我正在调整现有的库("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
Run Code Online (Sandbox Code Playgroud)

以下是相关的代码段.请注意,到目前为止,我只修改了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.

   */
  ...
}
Run Code Online (Sandbox Code Playgroud)

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

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

任何见解?

jxh*_*jxh 5

WiFly您的成员WiFlyClient正在使该课程无法转让.原因是赋值不能用于更改引用引用的对象.例如:

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
Run Code Online (Sandbox Code Playgroud)

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

// WiFlyClient.h
class WiFlyClient : public Client {
 public:
  WiFlyClient();
  ...
private:
  static WiFlyDevice& _WiFly;
  ...
};
Run Code Online (Sandbox Code Playgroud)

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

WiFlyDevice & WiFlyClient::_WiFly = WiFly;
Run Code Online (Sandbox Code Playgroud)