我正在修改一个现有的库("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对象被赋值时,但我不明白为什么它不能在这里工作,因为它不需要修改就能工作。
(是的,我知道头文件中有实现,我不知道他们为什么要这样写,不要怪我!)
有什么见解吗?
发布于 2012-08-18 04:23:08
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;发布于 2012-08-18 04:04:24
显然,问题在于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>发布于 2012-08-18 04:26:17
尝试覆盖operator=
WiFlyClient& operator= (const WiFlyClient & wi)
{
/* make a copy here */
return *this;
}https://stackoverflow.com/questions/12012277
复制相似问题