我有以下问题:
我想从main函数中的类创建对象。看来这是链接器的问题。你有什么想法,这条错误信息的目的是什么?
它的错误消息是:
-1:错误: LNK2019: stes外部符号“public:__thiscall Test::Test(class QString)”(??0 Test@QAE@VQString@Z) -1:错误: LNK2019: stes外部符号“public:__thiscall Test::~Test(void)”(?1 Test@QAE@XZ)在函数"_main“中。
调试\wth.exe:-1:错误: LNK1120: 2 nicht aufgel体外
我有非常简单的课堂考试:
test.h
#ifndef TEST_H
#define TEST_H
#include <QtCore/QObject>
class Test
{
public:
Test(QString name);
~Test();
private:
QString m_name;
};
#endif // TEST_H然后,.cpp文件如下所示:
test.cpp
#include "test.h"
Test::Test(QString st) : m_name(st){}
Test::~Test(){}非常基本,在我的主要功能中:
main.cpp
#include <QCoreApplication>
#include "test.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Test t("lam");
return a.exec();
}发布于 2016-09-16 12:33:38
很可能您正在寻找一个关于创建QObject类的示例。
让我们扩展您的代码:
#ifndef TEST_H
#define TEST_H
#include <QtCore/QObject>
class Test : public QObject
{
// Allways add the Q_OBJECT macro
Q_OBJECT
public:
// Use a null default parent
explicit Test(const QString& name, QObject* parent = 0);
~Test();
private:
QString m_name;
};
#endif // TEST_H在cpp文件中:
#include "test.h"
Test::Test(const QString& st, QObject* parent) : QObject(parent), m_name(st {}
Test::~Test(){}在你的main.cpp中没有
#include <QCoreApplication>
#include "test.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Test t("lam");
return a.exec();
}这应该能行。如果您有链接问题,请执行以下步骤:
发布于 2016-09-16 12:05:57
所以我不得不先运行qmake。我在做的是建造,而不是跑步。
谢谢大家,只是花了很多时间。我是新来的Qt。
发布于 2016-09-16 12:05:33
现在,您在main.cpp中包含了main.cpp,但是test.h没有引用test.cpp实现。因此,您必须将test.cpp包含在test.h的底部,或者将其包含在编译器调用中。
https://stackoverflow.com/questions/39530944
复制相似问题