1

I am new in c++ programming and also in QT.

I want to test Qt slots and signals and see how they work. I have a header file named myclass.h which has following code:

#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
#include <QString>
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 changedtext(const QString&);
     private:
     QString m_text;
};

and a file with class_mine name with the following code :

#include "myclass.h"
#include <QObject>
#include <QString>
void myclass::settext(const QString &text)
{
     if (m_text==text)
        return;
     m_text =text;
     emit changedtext(m_text);
} 
#endif

and in my main file I have this :

#include "mainwindow.h"
#include <QApplication>
#include "myclass.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    // MainWindow w;
    // w.show();
    QObject parent;
    myclass *a1 , *b , *c;
    a1=new myclass("foo" , &parent);
    b=new myclass("bar" , &parent);
    c=new myclass("baz" , &parent);
    QObject::connect(a1,SIGNAL(changedtext(const QString&)),
    b,SLOT(settext(const QString&)));
    QObject::connect(b,SIGNAL(changedtext(const QString&)),
    c,SLOT(settext(const QString&)));
    QObject::connect(c,SIGNAL(changedtext(const QString&)),
    b,SLOT(settext(const QString&)));
    b->settext("text");
    return a.exec();
}

I don't know why I'm getting this error:

main.cpp:13: error: undefined reference to `myclass::myclass(QString const&, QObject*)'

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 1
    being new C++ is not a good condition to learn QT – The Techel Apr 28 '17 at 11:51
  • 1
    I'd like to second what @TheTechel said. For one, Qt is big and can take time to master; you're effectively fighting on two fronts. Secondly, Qt uses C++ in a very particular way, which is in places rather distant from recommended modern C++ practices. I suggest you get a firmer grasp on C++ first (e.g. from a [good book](http://stackoverflow.com/q/388242/1782465)) before looking into Qt. With Qt, you want to be able to evaluate which of its practices are worth following in your code, and which are best isolated to just the Qt-interacting portions of it. – Angew is no longer proud of SO Apr 28 '17 at 11:57
  • thanks for your recomendation but I have to learn both of them at the same time. –  Apr 29 '17 at 07:58

1 Answers1

2

Define your constuctor in myclass.h

myclass(const QString &text, QObject *parent=0) { /* your code here */ };

or in myclass.cpp

myclass::myclass(const QString &text, QObject *parent) { /* your code here */ };
sovas
  • 1,508
  • 11
  • 23