Construct a class inside another class exept main.cpp not working
-
hi.
at the moment i have 6 files in my project (the project file and a main.cpp, an experiment.cpp and a tcpserver.cpp and the 2 *.h files belonging to those)
@#include <QtGui/QApplication>
#include "klassenexperiment.h"
#include "tcpserver.h"the main.cpp, where nothing else than standard and construction of one tcpserver object
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
klassenexperiment w;
w.show();
tcpserver server1;return a.exec();
}
@empty default klassenexperiment ui class
@#ifndef KLASSENEXPERIMENT_H
#define KLASSENEXPERIMENT_H#include <QMainWindow>
#include <QtDebug>
#include <QTcpSocket>
#include <QTcpServer>namespace Ui {
class klassenexperiment;
}class klassenexperiment : public QMainWindow
{
Q_OBJECTpublic:
explicit klassenexperiment(QWidget *parent = 0);
~klassenexperiment();public slots:
private:
Ui::klassenexperiment *ui;
};#endif // KLASSENEXPERIMENT_H
@and
@
#include "klassenexperiment.h"
#include "ui_klassenexperiment.h"
#include "tcpserver.h"klassenexperiment::klassenexperiment(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::klassenexperiment)
{
ui->setupUi(this);}
klassenexperiment::~klassenexperiment()
{
delete ui;
}
@and finally the tcpserver:
@
#ifndef TCPSERVER_H
#define TCPSERVER_H#include <QObject>
#include <QtDebug>
#include <QTcpSocket>
#include <QTcpServer>class tcpserver : public QObject
{
Q_OBJECT
public:
explicit tcpserver(QObject *parent = 0);
QTcpServer *server;
QTcpSocket *socket;signals:
public slots:
void newconny();
};#endif // TCPSERVER_H
@and
@
#include "tcpserver.h"tcpserver::tcpserver(QObject *parent) :
QObject(parent)
{
server = new QTcpServer (this);
connect(server,SIGNAL(newConnection()),this,SLOT(newconny()));
server->listen(QHostAddress::Any,123);
qDebug() << "BORN!";
}void tcpserver::newconny()
{
socket = server->nextPendingConnection();
}
@like this everything works. with my portscanner i see the port on 123 locally opened.
but now when i want to construct a tcpserver object inside the klassenexperiment object, its not working.
means, everything seems to work, but in fact, there is no port opened.
thats the code how i did it (and of course i removed the constructon tcpserver server1; in main.cpp)@
#include "klassenexperiment.h"
#include "ui_klassenexperiment.h"
#include "tcpserver.h"klassenexperiment::klassenexperiment(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::klassenexperiment)
{
tcpserver server2;
ui->setupUi(this);}
klassenexperiment::~klassenexperiment()
{
delete ui;
}
@port stays closed.
any idea why?
is it wrong to include an object like this within an other object?
how would you do that? constuct the object in main.cpp or in the ui class? -
When you create your tcpserver without using new, it will be freed at the end of the scope. When your constructor returns, the tcpserver does not exist anymore, it has been destructed, poped from the stack. A simple fix: @tcpserver* server2 = new tcpserver(this);@ Passing this ensures it will be properly deleted when the parent class (this) is destructed.
-
thanks, that helped a lot. it works now.
to the background:
why has it been destroyed? its an object i created.
a variable i create also doesn't get destroyed like this.to the style:
if later i have several classes, is it nicer to let them work together in the main.cpp or the ui-class.cpp? -
[quote author="safrano" date="1346338212"]why has it been destroyed? its an object i created.
a variable i create also doesn't get destroyed like this.[/quote]Of course it does. Everything that is created on the stack is destroyed when leaving the scope. Read up on stack vs heap, e.g. here: http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/