我是c ++的新手,我使用Qt在VS2010中有以下代码:
// test.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_test.h"
#include "testDevice.h"
#include "testControl.h"
class test : public QMainWindow
{
Q_OBJECT
public:
test(QWidget *parent = 0) : control(device,0) {}
~test();
private:
Ui::testClass ui;
testDevice device;
testControl control;
};
// test.cpp
#include "test.h"
test::test(QWidget *parent) : QMainWindow(parent)
{
ui.setupUi(this);
device.start();
control.start();
}
test::~test()
{
}
// testControl.h
#pragma once
#include <QThread>
#include "testDevice.h"
class testControl : public QThread
{
Q_OBJECT
public:
testControl(testDevice& device, QObject *parent = 0);
protected:
void run(void);
private:
testDevice device;
~testControl(void);
};
// testControl.cpp
#include "testControl.h"
testControl::testControl(testDevice& thisDevice, QObject *parent) : QThread(parent)
{
}
void testControl::run(void)
{
}
testControl::~testControl(void)
{
}
Run Code Online (Sandbox Code Playgroud)
VS2010说"没有合适的默认构造函数",它标志着:
test::test(QWidget *parent) : QMainWindow(parent)
{
Run Code Online (Sandbox Code Playgroud)
和
test::~test()
Run Code Online (Sandbox Code Playgroud)
作为错误来源.我曾尝试使用testControl作为指针并作为参考,但我还有很多其他错误......有人可以帮我解决这个问题并向我解释发生了什么事吗?
您在标头test的test类定义中提供了构造函数的隐式内联定义:
test(QWidget *parent = 0) : control(device,0) {}
Run Code Online (Sandbox Code Playgroud)
这很可能是编译器所抱怨的.除此之外,您还可以在文件中提供不同的定义.cpp.您只能有一个定义.
有两种方法可以解决这个问题.
1)在类定义中隐式内联构造函数的定义.修改现有的头代码以QMainWindow在初始化列表中调用相应的构造函数.并从.cpp文件中删除构造函数定义:
// in test.h
test(QWidget* parent = 0) : QMainWindow(parent), control(device, 0)
{
ui.setupUi(this);
device.start();
control.start();
}
Run Code Online (Sandbox Code Playgroud)
2)在头文件中声明构造函数,并在.cpp:中定义它:
// in test.h
test(QWidget* parent = 0); // declaration, no definition
// in test.cpp
test::test(QWidget* parent = 0) : QMainWindow(parent), control(device, 0)
{
ui.setupUi(this);
device.start();
control.start();
}
Run Code Online (Sandbox Code Playgroud)
这些解决方案都应该有效.你不能拥有的是两者的结合,这是你的代码样本所展示的.
您可能还需要定义test::~test().在您的代码示例中,您只显示声明.