Results 1 to 10 of 10

Thread: undefined reference

  1. #1
    Join Date
    May 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default undefined reference

    Hi there,
    as a complete newbie to both qt and C++ i am trying to reproduce the examples textfinder and addressbook.
    All goes well until i come to the slots part where i declare:

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

    The complete code in textfinder.h is below
    The debug error i get is:
    debug/moc_textfinder.o:C:\qtbron\textfinder/debug/moc_textfinder.cpp:72:
    undefined reference to `textfinder:n_findButton_clicked()'

    I guess it is enviremental but have no clue.

    Thanks in advance.


    Qt Code:
    1. #ifndef TEXTFINDER_H
    2. #define TEXTFINDER_H
    3.  
    4. #include <QWidget>
    5. #include <QObject>
    6.  
    7. namespace Ui {
    8. class textfinder;
    9. }
    10.  
    11. class textfinder : public QWidget {
    12. Q_OBJECT
    13. public:
    14. textfinder(QWidget *parent = 0);
    15. ~textfinder();
    16. protected:
    17. void changeEvent(QEvent *e);
    18. private:
    19. Ui::textfinder *ui;
    20. void loadTextFile();
    21. private slots:
    22. void on_findButton_clicked();
    23.  
    24. };
    25.  
    26. #endif // TEXTFINDER_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 18th May 2010 at 16:23. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference

    Did you actually implement textfinder:n_findButton_clicked() in your .cpp file?

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: undefined reference

    Post your cpp file if so.

  4. #4
    Join Date
    May 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference

    Here the cpp file, don't no how that smiley got there.
    The error i get writes :
    debug/moc_textfinder.o:C:\qtbron\textfinder/debug/moc_textfinder.cpp:72: undefined reference to `textfinder:n_findButton_clicked()'


    cpp:
    Qt Code:
    1. #include "textfinder.h"
    2. #include "ui_textfinder.h"
    3.  
    4.  
    5. textfinder::textfinder(QWidget *parent) :
    6. QWidget(parent),
    7. ui(new Ui::textfinder)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. textfinder::~textfinder()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void textfinder::changeEvent(QEvent *e)
    18. {
    19. QWidget::changeEvent(e);
    20. switch (e->type()) {
    21. case QEvent::LanguageChange:
    22. ui->retranslateUi(this);
    23. break;
    24. default:
    25. break;
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 18th May 2010 at 16:24. Reason: missing [code] tags

  5. #5
    Join Date
    May 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference

    This is weird, i do a simple copy/paste of the error output without smiley.
    Like this:
    debug/moc_textfinder.o:C:\qtbron\textfinder/debug/moc_textfinder.cpp:72: undefined reference to `textfinder:n_findButton_clicked()'
    press 'Submit Reply' and the smiley is there.

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: undefined reference

    You didn't implement the slot void on_findButton_clicked(); in cpp you should have something like this:
    Qt Code:
    1. void textfinder::on_findButton_clicked() {
    2. //... implementation of the function/slot goes here...
    3. }
    To copy to clipboard, switch view to plain text mode 

    To put code in message, place tags around the code like this: [ CODE] actual code [ /CODE] (without any space between [ ])
    Last edited by Zlatomir; 18th May 2010 at 14:45.

  7. #7
    Join Date
    May 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference

    Thanks Zlatomir and others, that worked. I have to read and learn some more, thought that your answer wasn't necessary with Auto-Connect.

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: undefined reference

    Auto-connect still needs a function to link to though, it can't guess what you want to do when you press that button

  9. #9
    Join Date
    May 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference

    Yes that's the part i understand. Btw i do my stuf up till now in visual basic.
    What i don't understand is where do i declare a slot. From the textfinderexample i thought in textfinder.h witch i posted earlier but then i get the moc_textfinder.h error.
    Now i declared them both in header and ccp, no errors now.
    Also i am still figuring out the C++ approuch with main, header, source and preprocessing.

    Thanks, Digidas.

  10. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: undefined reference

    A slot is the same as any other normal C++ method, the "slots:" part is just for MOC (the C++ preprocessor removes it before invoking the compiler)

    Normally you would put the prototype of the function (the signature) in the class declaration, and then the body of the function in your .cpp file. For small functions, sometimes the body is included in the class declaration, but I always think it looks better to have a clear seperation - prototypes and class declarations in header file, code in cpp file.

Similar Threads

  1. Undefined reference to my signal
    By tomek in forum Newbie
    Replies: 9
    Last Post: 1st December 2011, 08:14
  2. undefined reference
    By jayreddy in forum Qt Programming
    Replies: 1
    Last Post: 20th November 2009, 14:45
  3. Undefined reference to crt
    By derektaprell in forum Installation and Deployment
    Replies: 0
    Last Post: 20th October 2009, 09:34
  4. Undefined Reference To...
    By ManuMies in forum Qt Programming
    Replies: 6
    Last Post: 10th February 2009, 13:14
  5. Undefined reference
    By Salazaar in forum Newbie
    Replies: 12
    Last Post: 23rd May 2007, 11:21

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.