?
Solved

undefined reference, can't compile

Posted on 2010-01-08
4
Medium Priority
?
620 Views
Last Modified: 2013-11-23
I'm trying to run a code from the foundation of QT book but I can' compile it I get the error

MyClass *bridge = new MyClass( "", &app );
undefined reference to `MyClass::MyClass(QString const&, QObject*)
:-1: error: collect2: ld returned 1 exit status


#include <QtGui/QApplication>
#include <QString>
#include <QObject>
#include "mainwindow.h"
#include <qlineedit.h>
#include <QLabel>
#include <QVBoxLayout>

class MyClass : public QObject
{
    Q_OBJECT
    public:
        MyClass( const QString &text, QObject *parent = 0 );
        const QString& text() const;
        int getLengthOfText() const;
    public slots:
        void setText( const QString &text );
    signals:
        void textChanged( const QString& ) ;
    private:
        QString m_text;
};

int main( int argc, char **argv )
{
QApplication app( argc, argv );
QWidget widget;

QLineEdit *lineEdit = new QLineEdit;
QLabel *label = new QLabel;
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget( lineEdit );
layout->addWidget( label );
widget.setLayout( layout );

MyClass *bridge = new MyClass( "", &app );

QObject::connect(
lineEdit, SIGNAL(textChanged(const QString&) ),
bridge, SLOT(setText(const QString&) ) );
QObject::connect(
bridge, SIGNAL(textChanged(const QString&) ),
label, SLOT(setText(const QString&)) ) ;

widget.show();
return app.exec();
}

Open in new window

0
Comment
Question by:NewtonianB
4 Comments
 
LVL 17

Assisted Solution

by:CSecurity
CSecurity earned 100 total points
ID: 26211870
What's OS you are compiling in? If it's FreeBSD rename libqt2.so to libqt.so

Also don't forget to link with libqt.so
0
 
LVL 12

Assisted Solution

by:Let_Me_Be
Let_Me_Be earned 100 total points
ID: 26211907
Where is the definition of MyClass::MyClass( const QString &text, QObject *parent = 0 ); ?
I don't see any.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 100 total points
ID: 26212387
For that matter, you haven't implemented any of the MyClass member functions.
0
 
LVL 3

Accepted Solution

by:
pchela733 earned 1700 total points
ID: 26212443
Add all constructor and functions definitions:
#include <QtGui/QApplication>
#include <QString>
#include <QObject>
//#include "mainwindow.h"
#include <qlineedit.h>
#include <QLabel>
#include <QVBoxLayout>

class MyClass : public QObject
{
    Q_OBJECT
    public:
    MyClass( const QString &text, QObject *parent = 0 );
        const QString& text() const;
        int getLengthOfText() const;
    public slots:
        void setText( const QString &text );
    signals:
        void textChanged( const QString& ) ;
    private:
        QString m_text;
};

//////////////////////////////
MyClass::MyClass( const QString &text, QObject *parent )
{
    m_text = QString( "123" );
}

int MyClass::getLengthOfText() const
{
    return 0;
}

const QString& MyClass::text() const
{
    return m_text;
}

void MyClass::setText( const QString &text )
{
}

void MyClass::textChanged( const QString& )
{
}


///////////////////////////
int main( int argc, char **argv )
{
QApplication app( argc, argv );
QWidget widget;

QLineEdit *lineEdit = new QLineEdit;
QLabel *label = new QLabel;
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget( lineEdit );
layout->addWidget( label );
widget.setLayout( layout );

MyClass *bridge = new MyClass( "Hello", &app );

QObject::connect(
lineEdit, SIGNAL(textChanged(const QString&) ),
bridge, SLOT(setText(const QString&) ) );
QObject::connect(
bridge, SIGNAL(textChanged(const QString&) ),
label, SLOT(setText(const QString&)) ) ;

widget.show();
return app.exec();
}

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question