2011-01-07 44 views
0

也許我過於雄心勃勃,但我試圖編寫一個服務器程序,它可以通過QLocalSockets和QTcpSockets接受連接。這個概念是有一個「關係」的對象既具有QLocalServer和QTcpServer既可聆聽新的連接:將QLocalSocket *傳遞給期望QIODevice的方法*

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

的問題是,這將無法編譯,給了一個錯誤:

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

現在指向的類型顯然無關,和在我的代碼的其他地方我沒有問題,在所有類似的行動:

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

server = socket; 

...所以誰能告訴我,爲什麼編譯器和噸問題他?有沒有一種方法可以讓構造函數接受QLocalServer *?我想有個核心選項讓構造函數接受一個void指針加上一個額外的變量來告訴它它被髮送了什麼,所以它可以重新指定一個QLocalSocket或QTcpSocket的void指針,但是我覺得不適合使用reinterpret_cast看起來應該是C++多態性的一個簡單點。

Regards,

Stephen。

回答

1

最可能的原因是您忘記了發生錯誤的源文件中的#include <QLocalSocket>

+0

善良我,是的,它修復了它。謝謝。我想我假設它是在qlocalserver.h中引用的,但顯然不是。 – 2011-01-07 06:56:58

相關問題