1
#include <QtCore/QCoreApplication>
#include <QTCore>
#include <QtNetwork>
#include <QDebug>

#define CONNECT(sndr, sig, rcvr, slt) connect(sndr, SIGNAL(sig), rcvr, SLOT(slt))

class mynet : QObject
{
    Q_OBJECT

public:
    mynet()
    {}

    void start()
    {
        CONNECT(tcpServer,       newConnection(),                     this, acceptConnection());
        CONNECT(tcpClient,       connected(),                         this, startTransfer());
        CONNECT(tcpClient,       bytesWritten(qint64),                this, updateClientProgress(qint64));
        CONNECT(tcpClient,       error(QAbstractSocket::SocketError), this, displayError(QAbstractSocket::SocketError));

        // start server listening
        tcpServer->listen();
        while(!tcpServer->isListening());

        // make client connection
        tcpClient->connectToHost(QHostAddress::LocalHost, tcpServer->serverPort());
    }

public slots:
    void acceptConnection()
    {
        tcpServerConnection = tcpServer->nextPendingConnection();
        CONNECT(tcpServerConnection, readyRead(), this, updateServerProgress());
        CONNECT(tcpServerConnection, error(QAbstractSocket::SocketError), this, displayError(QAbstractSocket));
        tcpServer->close();
    }

    void startTransfer()
    {
        bytesToWrite = TotalBytes - (int)tcpClient->write(QByteArray(PayloadSize, '@'));
    }

    void updateServerProgress()
    {
        bytesReceived += (int)tcpServerConnection->bytesAvailable();
        tcpServerConnection->readAll();

        if (bytesReceived == TotalBytes)
        {
            qDebug() << "done";
            tcpServerConnection->close();
        }
    }

    void updateClientProgress(qint64 numBytes)
    {
        // callen when the TCP client has written some bytes
        bytesWritten += (int)numBytes;

        // only write more if not finished and when the Qt write buffer is below a certain size.
        if (bytesToWrite > 0 && tcpClient->bytesToWrite() <= 4*PayloadSize)
            bytesToWrite -= (int)tcpClient->write(QByteArray(qMin(bytesToWrite, PayloadSize), '@'));
    }

    void displayError(QAbstractSocket::SocketError socketError)
    {
        if (socketError == QTcpSocket::RemoteHostClosedError)
            return;

        qDebug() << tcpClient->errorString();


        tcpClient->close();
        tcpServer->close();
    }

private:
    QTcpServer* tcpServer;
    QTcpSocket* tcpClient;
    QTcpSocket* tcpServerConnection;
    int bytesToWrite;
    int bytesWritten;
    int bytesReceived;
    int TotalBytes;
    int PayloadSize;
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    mynet m1;
    m1.start();

    return a.exec();
}

私は得る

Undefined symbols for architecture x86_64:
"vtable for mynet", referenced from:
  mynet::mynet() in main.o
  mynet::~mynet()in main.o. 

私が間違っていることを教えてください。Qtで何らかの理由でクラスのメソッド定義をインライン化できませんか?

4

4 に答える 4

1

クラスを.proファイルに追加する必要があります

HEADERS += mynet.h
SOURCES += mynet.cpp

そのため、メタオブジェクトコンパイラはそれらをスキャンして、モーキングが必要であると判断し、関連するスタブを生成できます。

于 2012-05-25T12:24:51.450 に答える
1

必ずネットワークを .pro ファイルに追加してください。これにより、ネットワーク ライブラリ関数への正しいリンクが作成されます。

QT       += core network
于 2012-08-10T06:53:19.377 に答える
0

2つのこと:

1) QObject から公に派生する必要があります。

2)このファイルをモックしてから、出力をコンパイルしてリンクしていますか?Q_OBJECT マクロを含めて moc しないと、そのようなエラーが発生します。

于 2012-05-14T03:58:50.973 に答える