使用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:错误: 对" myClass :: myClass(qString const&amp;,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 */ };
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用