使用QT时未定义对构造函数的引用

Undefined reference to constructor when using QT

我是 C++ 编程新手,也是 QT 新手。

我想测试 Qt 槽和信号,看看它们是如何工作的。我有一个名为 myclass.h 的头文件,其中包含以下代码:

#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;
};

和一个名称为 class_mine 的文件,代码如下:

#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

在我的主文件中有这个:

#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();
}

我不知道为什么会出现此错误:

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

在 myclass.h

中定义构造函数
myclass(const QString &text, QObject *parent=0) { /* your code here */ };

或在myclass.cpp

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