Results 1 to 15 of 15

Thread: undefined reference to MainWindow::server

  1. #1
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default undefined reference to MainWindow::server

    I declare this in MainWindow.h

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    static serversocket *server;

    and in MainWindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);


    // serversocket *server = new serversocket();
    bool success = server->listen(QHostAddress::Any, 4200);

    compiler show the follow error
    mainwindow.cpp:14: error: undefined reference to `MainWindow::server'

    i don't wana declare server as public variable

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: undefined reference to MainWindow::server

    Looks more like a linker error to me. Where is class serversocket implemented and is that part of the link?
    Might help to know what platform and toolchain.

    i don't wana declare server as public variable
    Good. Why declare it static? Why use the pointer uninitialised?
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. #3
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference to MainWindow::server

    I Use Qt Creator 2.4.1 Based on Qt 4.7.4 (32 bit), in linux enviroment, I use GCC x86 32 bits,

    in mainwindow.cpp I have #include "serversocket.h" in mainwindow.h I have include"serversocket.h" too.

    If i declare this function as "No Static" I get other error in other form named as logform

    void logform::closeEvent(QCloseEvent *event) {

    //MainWindow.server
    MainWindow::server->LogComunicaoAtivo = false;

    // msgi(MainWindow::server->);


    }

    error: invalid use of non-static data member 'MainWindow::server'

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: undefined reference to MainWindow::server

    Where is the implementation of class serversocket? Is it in a separate library? Is it part of the project?

    If i declare this function as "No Static" I get other error in other form named as logform
    Yes, you do. To access a public member variable of function you need an instance of MainWindow (pointer, reference or actual instance). Perhaps you should pass one as a parameter to the other form. This is basic C++ knowledge and nothing to do with Qt.

  5. #5
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference to MainWindow::server

    serversocket is part of project

    Qt Code:
    1. {
    2. Q_OBJECT
    3.  
    4. public:
    5. serversocket(QObject *parent=0);
    6. logform *varLogForm;
    7. bool LogComunicaoAtivo;
    8.  
    9. private slots:
    10. void readyRead();
    11. void disconnected();
    12. void sendUserList();
    13.  
    14. protected:
    15. void incomingConnection(int socketfd);
    16.  
    17.  
    18. private:
    19. QSet<QTcpSocket*> clients;
    20. QMap<QTcpSocket*,QString> users;
    21.  
    22. void addLog(QString pText);
    23.  
    24. };
    25.  
    26. #endif // SERVERSOCKET_H
    To copy to clipboard, switch view to plain text mode 

    I think than you is right about C++, i newby in C++ too,

    in main unit i create main window like below

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

    MainWindow w;
    w.show();


    return a.exec();
    }

    how can I create a MainWindow as new instance instead this way above? I wanna server declared like a public and all other forms need access to him.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: undefined reference to MainWindow::server

    I assume that all the other forms are created by your MainWindow at one time or another. Here is one approach:
    Qt Code:
    1. class MainWindow: public QMainWindow {
    2. Q_OBJECT
    3.  
    4. public:
    5. MainWindow(QWidget *p = 0):
    6. m_server(new serversocket(this)) // the private server created here
    7. {
    8. ...
    9. }
    10. private:
    11. serversocket *m_server;
    12. };
    13.  
    14. // and
    15. class Form: public QWidget {
    16. Q_OBJECT
    17. public:
    18. Form(serversocket *server, QWidget *p = 0): QWidget(p), m_server(server)
    19. { } // pointer to server kept here
    20.  
    21. void doStuff() {
    22. m_server->doNetworkyThings() // pointer to server used here
    23. };
    24. private:
    25. serversocket *m_server;
    26. ...
    27. };
    To copy to clipboard, switch view to plain text mode 
    Then when you create a form in a MainWindow function that needs direct access to the server object:
    Qt Code:
    1. ...
    2. Form *form = new Form(m_server, this); // pointer to server passed into form
    3. ...
    To copy to clipboard, switch view to plain text mode 

    You may want to ask yourself is exposing the server directly is the best solution.

  7. #7
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference to MainWindow::server

    thanks my friend, but i found a more elegant way to do it.

    http://www.codeproject.com/Articles/...ntation-with-C

    is more easy and clean that your way, but thanks so much for your coperation.

    the final result is this

    bool success = serversocket::getInstance()->listen(QHostAddress::Any, 4200);

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: undefined reference to MainWindow::server

    Using a singleton pattern is one option that works. The pattern potentially comes with downsides in some circumstances that might be worth a few minutes research to see if they apply to you.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: undefined reference to MainWindow::server

    There are people who say that if one has to use the singleton pattern to solve his problem, it usually means the design of the software is broken. As it seems to be here as I don't see any reason for a socket to be a singleton class. In this particular situation it seems to me that the problem is trying to access the socket from within close event (or any other method) of a UI window which does not own the server object. IMHO instead of this a signal should be emitted and caught in some kind of controller having direct access to the server instance.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference to MainWindow::server

    Dear wysota, I understand your viewpoint, but do you have some suggestion for make class serversocket viewed in any class and form of project? I prefere declare serversocket like a public in mainwindows ?

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: undefined reference to MainWindow::server

    Quote Originally Posted by rz View Post
    but do you have some suggestion for make class serversocket viewed in any class and form of project?
    I have a suggestion to not try and make the serversocket instance visible in any class of the project. This breaks at least one of the fundamental object oriented programming rules.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference to MainWindow::server

    and if I have , any forms and just one connection! how can I do IT ? i need any forms see this connection!

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: undefined reference to MainWindow::server

    Why do you "need" all forms to see this connection?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference to MainWindow::server

    beacause I wanna see log of comunication in one form, when I close form I need set a flag do disable Log,
    in other form I wanna test comunication, and due any more reasons from project

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: undefined reference to MainWindow::server

    So pass a pointer to the socket manager to each form when you create them.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. The following user says thank you to wysota for this useful post:

    rz (30th March 2014)

Similar Threads

  1. undefined reference
    By deepakswaroop in forum Newbie
    Replies: 1
    Last Post: 2nd March 2011, 06:46
  2. undefined reference
    By jayreddy in forum Qt Programming
    Replies: 1
    Last Post: 20th November 2009, 13:45
  3. Undefined Reference To...
    By ManuMies in forum Qt Programming
    Replies: 6
    Last Post: 10th February 2009, 12:14
  4. Undefined reference
    By Salazaar in forum Newbie
    Replies: 12
    Last Post: 23rd May 2007, 10:21
  5. Replies: 5
    Last Post: 14th February 2006, 23:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.