错误:非静态引用成员,无法使用默认赋值运算符
Error: non-static reference member, can't use default assignment operator
我正在调整一个现有的库("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();
,如果我指出问题消失了(但整个事情都依赖于这一行)。实际的问题似乎是,当WiFiClient对象被分配时,_WiFly成员无法初始化(覆盖?),但我不明白为什么它在没有修改的情况下工作时不能在这里工作。
(是的,我知道头文件中有实现,我不知道他们为什么这么写,别怪我!)
有什么见解吗?
WiFlyClient
的WiFly
成员使类不可赋值。原因是赋值不能用于更改引用所指的对象。例如:
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;
显然问题在于WiFlyClient
是不可赋值的。考虑将它包含在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>
尝试覆盖operator=
:
WiFlyClient& operator= (const WiFlyClient & wi)
{
/* make a copy here */
return *this;
}
相关文章:
- 默认赋值运算符如何在实际 STL 中实现
- 如果类在 C++ 中具有常量或引用类型的非静态数据成员,为什么编译器不提供默认赋值运算符?
- C++不同类型的默认赋值运算符
- 将默认赋值运算符声明为 constexpr:哪个编译器是正确的?
- 默认赋值运算符有权访问基类的私有成员
- 错误:非静态引用成员"std::ostream&Student::out",无法使用默认赋值运算符
- 默认的复制构造函数和复制赋值运算符给出奇怪的错误
- 在这种情况下,我们需要禁用默认的复制构造函数和赋值运算符
- 错误:非静态引用成员,无法使用默认赋值运算符
- 如果成员具有非平凡的noexcept赋值运算符,则默认的移动赋值不能显式为noexcept
- 外部C结构的C++默认复制/移动赋值运算符不是常量
- 如果类具有引用数据成员,为什么编译器不合成默认赋值运算符
- 模板赋值运算符不替换默认赋值运算符
- 删除默认C++复制和移动构造函数和赋值运算符的缺点
- 具有不同模板参数的模板类的默认赋值运算符
- 重载赋值运算符或使用默认运算符
- 为什么我不能在 C++ 中使用模板版本覆盖默认的复制构造函数和赋值运算符
- c++默认的复制和赋值运算符
- 当类成员是引用时,是否无法生成默认赋值运算符?(在C++中)
- "非静态引用成员,不能使用默认赋值运算符"