PDA

View Full Version : undefined reference



digidas
17th May 2010, 16:27
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:


private slots:
void on_findButton_clicked();

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::on_findButton_clicked()'

I guess it is enviremental but have no clue.

Thanks in advance.



#ifndef TEXTFINDER_H
#define TEXTFINDER_H

#include <QWidget>
#include <QObject>

namespace Ui {
class textfinder;
}

class textfinder : public QWidget {
Q_OBJECT
public:
textfinder(QWidget *parent = 0);
~textfinder();
protected:
void changeEvent(QEvent *e);
private:
Ui::textfinder *ui;
void loadTextFile();
private slots:
void on_findButton_clicked();

};

#endif // TEXTFINDER_H

victor.fernandez
17th May 2010, 17:15
Did you actually implement textfinder::on_findButton_clicked() in your .cpp file?

squidge
17th May 2010, 19:35
Post your cpp file if so.

digidas
18th May 2010, 13:24
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::on_findButton_clicked()'


cpp:

#include "textfinder.h"
#include "ui_textfinder.h"


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

textfinder::~textfinder()
{
delete ui;
}

void textfinder::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

digidas
18th May 2010, 13:31
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::on_findButton_clicked()'
press 'Submit Reply' and the smiley is there.

Zlatomir
18th May 2010, 13:40
You didn't implement the slot void on_findButton_clicked(); in cpp you should have something like this:


void textfinder::on_findButton_clicked() {
//... implementation of the function/slot goes here...
}



To put code in message, place tags around the code like this: [ CODE] actual code [ /CODE] (without any space between [ ])

digidas
18th May 2010, 14:08
Thanks Zlatomir and others, that worked. I have to read and learn some more, thought that your answer wasn't necessary with Auto-Connect.

squidge
18th May 2010, 18:35
Auto-connect still needs a function to link to though, it can't guess what you want to do when you press that button :)

digidas
19th May 2010, 11:51
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.

squidge
19th May 2010, 13:04
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.