将 QLocalSocket* 传递给需要 QIODevice* 的方法

发布于 2024-10-10 10:30:47 字数 2197 浏览 6 评论 0原文

也许我过于雄心勃勃,但我正在尝试编写一个可以接受 QLocalSockets 和 QTcpSockets 上的连接的服务器程序。这个概念是有一个“连接”对象,其中 QLocalServer 和 QTcpServer 都侦听新连接:

    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: '计算服务器::计算服务器(QIODevice *,QObject *)' : 无法将参数 1 从 'QLocalSocket *' 转换为 'QIODevice *'1>尖头类型 不相关;转换需要 reinterpret_cast,C 风格强制转换或 函数式转换

显然不是不相关的,并且在我的代码中的其他地方,我对以下操作完全没有问题:

QLocalSocket *socket = new QLocalSocket;
QIODevice    *server = new QIODevice;

server = socket;

...所以任何人都可以告诉我为什么编译器有问题与这个?有没有办法让构造函数接受 QLocalServer* ?我想有一个核心选​​项是让构造函数接受一个 void 指针加上一个额外的变量来告诉它正在发送什么,这样它就可以将 void 指针重新转换为 QLocalSocket 或 QTcpSocket,但我对求助于reinterpret_cast感到不舒服看起来它应该是一个简单的 C++ 多态性。

问候,

斯蒂芬。

Perhaps I'm being over-ambitious, but I'm trying to write a server program which can accept connections over both QLocalSockets and QTcpSockets. The concept is to have a 'nexus' object with both a QLocalServer and QTcpServer listening for new connections:

    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);
    }

... and then separate slots which establish a server object, whose constructor takes a pointer to a QIODevice. In theory, this ought to work because both QLocalSocket and QTcpSocket inherit QIODevice. Here is the newLocalConnection slot, for example:

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();
}

The problem is that this won't compile, giving an error:

error C2664:
'CalculationServer::CalculationServer(QIODevice
*,QObject *)' : cannot convert parameter 1 from 'QLocalSocket *' to
'QIODevice *' 1> Types pointed
to are unrelated; conversion requires
reinterpret_cast, C-style cast or
function-style cast

Now the types pointed to are clearly not unrelated, and elsewhere in my code I have no problems at all with actions like:

QLocalSocket *socket = new QLocalSocket;
QIODevice    *server = new QIODevice;

server = socket;

... so can anyone tell me why the compiler has a problem with this? Is there a way that I can make the constructor accept the QLocalServer*? I suppose there is the nuclear option of getting the constructor to take a void pointer plus an extra variable to tell it what it's being sent, so it can then recast the void pointer to either a QLocalSocket or QTcpSocket, but I feel uncomfortable resorting to reinterpret_cast on what looks like it ought to be a straightforward bit of C++ polymorphism.

Regards,

Stephen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛歌少女 2024-10-17 10:30:47

最可能的原因是您忘记在发生错误的源文件中添加#include

The most likely reason is that you have forgotten to #include <QLocalSocket> in the source file where the error occurs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文