Joe B.
Joe B.

Reputation: 1

Declaring a pointer to a class object within a class declaraion

I'm not a C++ guy, so some simple things still trip me up. I am using Qt and trying to get my Qt project done (it will be used to test a TCP/IP connection I am installing on a proto board).

In my mainwindow class, I want to include pointers to the client & server objects that get created when you push a button on the main window form. But when I put that declaration in the class, it doesn't appear as though the class knows what it is, even though I have included the header file.

Here's a snippet of my mainwindow class:

private:
    Ui::MainWindow *ui;
    EchoServer *mServer;
    EchoClient *mClient;
...

The EchoServer & Client lines get a syntax error and then missing type specifiers.

But when I put that same declaration in the mainwindow.cpp file in the button push event, there's no error!? Here a snippet of that function:

void MainWindow::on_pushButtonConnect_clicked()
{
    const ushort port = 9999;
    EchoServer *mServer;

    EchoServer server((const QString)ui->lineEdit_IP_Addr->text(), port, ui );
    EchoClient client((const QString)ui->lineEdit_IP_Addr->text(), port, ui );
    ...

Any idea, what am I missing?

Upvotes: 0

Views: 129

Answers (1)

vahancho
vahancho

Reputation: 21258

You can simply use the forward declaration in your header file like:

class EchoServer;
class EchoClient;

class MyClass
{

private:
    Ui::MainWindow *ui;
    EchoServer *mServer;
    EchoClient *mClient;
};

Upvotes: 2

Related Questions