将qlocalsocket*传递给期望qiodevice*的方法
-
30-09-2019 - |
题
也许我很雄心勃勃,但是我正在尝试编写一个服务器程序,该程序可以接受Qlocalsockets和qtcpsockets上的连接。这个概念是要与Qlocalserver和qtcpserver侦听新连接的“ Nexus”对象:
Nexus::Nexus(QObject *parent)
: QObject(parent)
{
// Establish a QLocalServer to deal with local connection requests:
localServer = new QLocalServer;
connect(localServer, SIGNAL(newConnection()),
this, SLOT(newLocalConnection()));
localServer -> listen("CalculationServer");
// Establish a UDP socket to deal with discovery requests:
udpServer = new QUdpSocket(this);
udpServer -> bind(QHostAddress::Any, SERVER_DISCOVERY_PORT);
connect(udpServer, SIGNAL(readyRead()),
this, SLOT(beDiscovered()));
// Establish a QTcpServer to deal with remote connection requests:
tcpServer = new QTcpServer;
connect(tcpServer, SIGNAL(newConnection()),
this, SLOT(newTcpConnection()));
tcpServer -> listen(QHostAddress::Any, SERVER_COMMAND_PORT);
}
...然后单独建立服务器对象的插槽,其构造函数将指针指向QIODEVICE。从理论上讲,这个 应该 因为qlocalsocket和qtcpsocket都继承了qiodevice。这是NewLocalconnection插槽,例如:
void Nexus::newLocalConnection()
{
// Create a new CalculationServer connected to the newly-created local socket:
serverList.append(new CalculationServer(localServer -> nextPendingConnection()));
// We don't allow more than one local connection, so stop listening on the server:
localServer -> close();
}
问题在于,这不会编译,给出错误:
错误c2664:“计算器:: calculationServer(qiodevice *,qobject *)':无法从'qlocalsocket *'转换参数1到'qiodevice *'1>指向指向的类型是无关的;转换需要Reinterpret_cast,C风格的铸造或功能风格的铸件
现在指向的类型显然是 不是 无关的,在我的代码中的其他地方,我对以下操作完全没有问题:
QLocalSocket *socket = new QLocalSocket;
QIODevice *server = new QIODevice;
server = socket;
...那么谁能告诉我为什么编译器对此有问题?有没有办法使构造函数接受qlocalserver*?我想有一个核选项,可以让构造函数采用无效的指针加上一个额外的变量来告诉它它正在发送什么,因此它可以将void指针重新铸造为qlocalsocket或qtcpsocket,但我感到不舒服,以求助于reinterpret_cast关于它的样子,应该是C ++多态性的直接一点。
问候,
斯蒂芬。
解决方案
最可能的原因是您忘记了 #include <QLocalSocket>
在发生错误的源文件中。
不隶属于 StackOverflow