Results 1 to 11 of 11

Thread: Public routines

  1. #1
    Join Date
    Feb 2012
    Posts
    33
    Qt products
    Qt4

    Default Public routines

    I have a routine in my MainWindow, and I declared public this routine:


    In MainWindow.cpp

    Qt Code:
    1. void MainWindow::Read_Ambiente()
    2. {
    3. }
    To copy to clipboard, switch view to plain text mode 


    In MainWindow.h

    Qt Code:
    1. public:
    2. void Read_Ambiente();
    To copy to clipboard, switch view to plain text mode 



    In another form I want to use this routine:

    In Diagnostica.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. extern void Read_Ambiente();
    To copy to clipboard, switch view to plain text mode 


    If I don't use this routine "Read_Ambiente" in the second form, there are no errors.

    When I use the routine, I have this error from the linker:

    diagnostica.obj:-1: error: LNK2019: unresolved external symbol "void __cdecl Read_Ambiente(void)" (?Read_Ambiente@@YAXXZ) referenced in function "private: virtual bool __thiscall Diagnostica::eventFilter(class QObject *,class QEvent *)" (?eventFilter@Diagnostica@@EAE_NPAVQObject@@PAVQEv ent@@@Z)
    Last edited by wysota; 5th April 2012 at 12:26. Reason: missing [code] tags

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Public routines

    There is no extern void Read_Ambiente(). There is only void MainWindow::Read_Ambiente().
    You need to make your MainWindow based object visible to other classes (preferebly as a constructor argument SomeClass::SomeClass(MainWindow *mainWindow)) and call its method.

  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Public routines

    Qt Code:
    1. // mainwindow.h
    2. class MainWindow: public QMainWindow
    3. {
    4. public:
    5. void readAmbiente();
    6. };
    7.  
    8. // mainwindow.cpp
    9. void MainWindow::readAmbiente()
    10. {
    11. // ...
    12. }
    13.  
    14.  
    15. // diagnostics.cpp
    16. mainWindow->readAmbiente(); // mainWindow is the instance of MainWindow
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2012
    Posts
    33
    Qt products
    Qt4

    Default Re: Public routines

    I have already tried it as written by "mentalmushroom" but the linker does not recognize Read_Ambiente() like MainWindow member.

    I must probably try like written by "mvuori", but I am not able to create a constructor in the MainWindow class and so finally call the class method.

    Can someone explain in detail what should I do?

    Thanks.

  5. #5
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Public routines

    At the very first show how did you do this.

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

    Default Re: Public routines

    Quote Originally Posted by eltecprogetti View Post
    I have already tried it as written by "mentalmushroom" but the linker does not recognize Read_Ambiente() like MainWindow member.
    Does it not recognize the member function or the object you're calling the method on? And the linker or the compiler? If the linker breaks the build process then most likely you didn't implement the method body (check that the signature of the implementation matches exactly the one in the declaration).
    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.


  7. #7
    Join Date
    Feb 2012
    Posts
    33
    Qt products
    Qt4

    Default Re: Public routines

    I was wrong, the error is given by the compiler.

    When I write the call, the QT automatically recognizes the MainWindow routine, so that is writed by pressing enter.
    But when the program is compiled, it report a syntax error: missing ';' before '.'

    If I write "MainWindow.Read_Ambiente ();" or "MainWindow->Read_Ambiente ();" is equal, the error is the same.

  8. #8
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Public routines

    Maybe you forgot to include mainwindow.h to the source file where you try to call its method. Show the code if you want to get a specific answer.

  9. #9
    Join Date
    Feb 2012
    Posts
    33
    Qt products
    Qt4

    Default Re: Public routines

    I resolved.

    it was necessary to create a istance of MainWindow in the Diagnostics.cpp (Mainwindow *Main).

    At this point I need to write Main->Read_Ambiente();

  10. #10
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Public routines

    of course, it is necessary, hard to imagine how could you use it without having an instance

  11. #11
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Public routines

    In some cases you could use 'static' method to call it wthout instance of an object.

Similar Threads

  1. Public variables
    By eltecprogetti in forum Qt Programming
    Replies: 8
    Last Post: 6th April 2012, 12:58
  2. Replies: 1
    Last Post: 27th August 2009, 05:41
  3. Qt Public Repository Launched!
    By lpotter in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2009, 00:38
  4. checksum routines?
    By gfunk in forum Qt Programming
    Replies: 2
    Last Post: 20th September 2007, 00:26
  5. Qt and CGI routines
    By jcr in forum Qt Programming
    Replies: 6
    Last Post: 28th November 2006, 06:15

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.