在Qt 5.7 c++中创建简单对象
Creating simple object in Qt 5.7 C++
我有以下问题:
我想在main函数中从类创建对象。看起来是连接器的问题。您知道这个错误消息的原因是什么吗?
它的错误信息是:
主要。obj:-1:错误:LNK2019: Verweis auf夜aufgelöstes外部符号"public: __thiscall Test::Test(类QString)"(? ? 0 test@@qae@vqstring@@@z)";in function "_main"
主要。obj:-1:错误:LNK2019: Verweis auf夜aufgelöstes外部符号"public: __thiscall Test::~Test(void)"(? ? 1 test@@qae@xz)";in function "_main"
debugWth.exe:-1:错误:LNK1120: 2夜aufgelöste外部
我有一个非常简单的Class Test:
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函数中我有:
main.cpp
#include <QCoreApplication>
#include "test.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Test t("lam");
return a.exec();
}
您可能正在寻找一个关于创建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(){}
No in your main.cpp
#include <QCoreApplication>
#include "test.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Test t("lam");
return a.exec();
}
这应该可以工作。如果您有链接问题,请执行以下步骤:
- 运行qmake(在你的项目文件夹中使用qt创建器的上下文菜单)
- 清理项目
- 重新构建
所以我必须先运行qmake。我所做的就是建造,然后跑。
谢谢大家,只是花了很多时间。我是Qt的新手
现在你在main.cpp
中包括test.h
,但test.h
没有引用test.cpp
的实现。因此,您必须将test.cpp
包含在test.h
的底部或将其包含在编译器调用中。
相关文章:
- 在Visual Studio 2017中创建的简单DLL不会在XP中加载
- 尝试创建包含简单方程的函数
- 在C++中创建文件夹选取器对话框的最简单方法是什么?
- 是否可以使用简单队列创建优先级队列
- 在 C/C++ 中,是否可以通过使用指针更改"important"内存地址的值来创建简单的恶意软件?
- 如何从C++源代码创建简单的应用
- 如何创建简单的CPU基准
- 为项目和循环创建简单的控制菜单会比预期打印提示的次数更多
- 在C 中创建简单类型
- 在C 中创建简单的文本编辑器
- 使用本机/C++代码在VS2010中创建简单COM对象的最简单方法是什么
- linux下的Deezer原生API:Unanble创建简单的应用程序
- 在C++/Qt中创建简单的WebService(充当服务器),提供JSON数据
- 使用哪个库来创建简单的端口扫描程序
- 语法糖:自动创建简单的函数对象
- 在c++中创建简单线程时出错
- 在QT中创建简单的时钟
- 在Qt 5.7 c++中创建简单对象
- 无法创建简单归档
- Qt创建简单的矩形板