简体   繁体   English

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

[英]Error: non-static reference member, can't use default assignment operator

I'm adapting an existing library ("Webduino", a web server for Arduino) to work with another existing library ("WiFly", a wifi module) and running into a problem. 我正在调整现有的库(“Webduino”,Arduino的Web服务器)与另一个现有的库(“WiFly”,一个wifi模块)一起工作并遇到问题。 Each library works fine on its own. 每个图书馆都可以自行运作。 The Webduino library expects to use an Ethernet hardware module over SPI, whereas the WiFi module uses a serial port (UART). Webduino库期望通过SPI使用以太网硬件模块,而WiFi模块使用串行端口(UART)。 The error I get is: 我得到的错误是:

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

Here are the relevant code snippets. 以下是相关的代码段。 Note that so far I have only been modifying WiFlyWebServer.h (Webduino): 请注意,到目前为止,我只修改了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.

   */
  ...
}

The problem stems from m_client = m_server.available(); 问题源于m_client = m_server.available(); , if I comment that out the problem goes away (but the whole thing relies on that line). 如果我评论说问题消失了(但整个事情依赖于那条线)。 The actual problem seems to be that the _WiFly member can't be initialized (overwritten?) when the WiFiClient object is being assigned, but I can't understand why it doesn't work here when it does work without modification. 实际问题似乎是在分配WiFiClient对象时,_WiFly成员无法初始化(覆盖?),但我无法理解为什么它在没有修改的情况下工作时不起作用。

(Yes, I know there's implementation in the header file, I don't know why they wrote it that way, don't blame me!) (是的,我知道头文件中有实现,我不知道为什么他们这样写,不要怪我!)

Any insights? 任何见解?

The WiFly member of your WiFlyClient is making the class not assignable. WiFly的成员WiFlyClient正在使类不可转让。 The reason for that is that assignment cannot be used to change which object the reference is referring to. 原因是赋值不能用于更改引用引用的对象。 For example: 例如:

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

Since all your WiFlyClient s are referencing the same WiFlyDevice instance, you can change WiFlyClient as the compiler suggests to use a static member: 由于所有WiFlyClient都引用相同的WiFlyDevice实例,因此您可以更改WiFlyClient因为编译器建议使用静态成员:

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

Then, you do not initialize it in the constructor, but in a source file where you define it. 然后,您不在构造函数中初始化它,而是在您定义它的源文件中。

WiFlyDevice & WiFlyClient::_WiFly = WiFly;

Clearly the problem is that WiFlyClient is not assignable. 显然问题是WiFlyClient不可分配。 Consider containing it in a std::unique_ptr (in C++03, std::auto_ptr ) so that you can at least assign items of that type. 考虑在std::unique_ptr (在C ++ 03, std::auto_ptr )中包含它,以便至少可以分配该类型的项目。

std::unique_ptr<WiFlyClient> m_client;

...

m_client = m_server.available();

...

// convert m_client.<...> to m_client-><...>

// change m_server.available() to return std::unique_ptr<WiFlyClient>

Try overriding operator= : 尝试重写operator=

WiFlyClient& operator= (const WiFlyClient & wi)
{
  /* make a copy here */ 
  return *this;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:非静态引用成员 - 不能使用默认赋值运算符 - error: non-static reference member - can't use default assignment operator '非静态引用成员,不能使用默认赋值运算符' - 'non-static reference member, can't use default assignment operator' 非静态参考成员&#39;int&Property <int> :: value&#39;,不能使用默认赋值运算符 - non-static reference member ‘int& Property<int>::value’, can’t use default assignment operator 错误:非静态引用成员&#39;std :: ostream&Student :: out&#39;,不能使用默认赋值运算符 - error: non-static reference member 'std::ostream& Student::out', can't use default assignment operator &#39;类型非静态const成员不能使用默认赋值运算符&#39; - 这是什么意思? - 'Type non-static const member can't use default assignment operator' - what does this mean? 非静态 const 成员,不能使用默认赋值运算符 - Non-static const member, can't use default assignment operator QtConcurrent错误:引用非静态成员 - QtConcurrent error: reference to non-static member 具有非静态lambda成员的类不能使用默认模板参数? - Class with non-static lambda member can't use default template paramers? 错误:无效使用非静态成员 - error: invalid use of non-static member 为非静态引用成员类编写`operator =` - writing `operator=` for non-static reference member class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM