0

I have declared an int function in mainwindow.h

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    int port();

furthermore I declared this function in the appendant mainwindow.cpp file.

int MainWindow::port()
{
    int port_int;
    QString port_ei;

    if(ui->portInput->text() == 0)
    {
        port_ei = "6008";
        ui->textIncome->setPlainText(port_ei);
        port_int = port_ei.toInt();
    }
    else
    {
        port_ei = ui->portInput->text();
        ui->textIncome->setPlainText(port_ei);
        port_int = port_ei.toInt();
    }

    return port_int;
}

now i want my server (in the myserver.cpp file) to listen to that port.

MyServer::MyServer(QObject *parent) :
    QObject(parent)
{
    server = new QTcpServer(this);
    connect(server,SIGNAL(newConnection()),this,SLOT(newConnection()));

    int port_out =  MainWindow::port();

    if(!server->listen(QHostAddress::Any,port_out))
    {

        qDebug() << "Server could not start.";

    }
    else
    {
        qDebug() << "Server started.";
    }
}

but qt tells me that i made an illegal request on a not static function (on the int port_out = MainWindow::port(); line). how to solve that? and is there a better way to do this, if you got two seperate .cpp and .h files (besides the main.cpp). yes i included "mainwindow.h" and "myserver.h" in both cpp files.

4

1 回答 1

1

MainWindow::port(); is a static function call. But port function is not static, it needs to be called like main_window->port() where main_windowis a pointer to a MainWindow object.

于 2013-07-01T14:10:58.163 回答