简体   繁体   English

如何初始化C ++类成员

[英]How to initialize a c++ class member

I am new to c++ and I have the following code in VS2010 using Qt: 我是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)
{
}

VS2010 is saying "no appropriate default constructor available" and it marks: VS2010说“没有合适的默认构造函数可用”,它标记为:

test::test(QWidget *parent) : QMainWindow(parent)
{

and

test::~test()

as the error source. 作为错误源。 I have tried to use testControl as a pointer and as a reference but I got many other errors...could someone please help me out with this one and explain to me whats going on? 我试图将testControl用作指针和参考,但是我遇到了许多其他错误...有人可以帮我解决这个问题,并向我解释发生了什么吗?

You are providing an implicitly inline definition of test 's constructor in the test class definition in the header: 您正在标题的test类定义中提供test的构造函数的隐式内联定义:

test(QWidget *parent = 0) : control(device,0) {}

This is most likely what the compiler is complaining about. 这很可能是编译器所抱怨的。 Besides that, you go on to provide a different definition in the .cpp file. 除此之外,您还要在.cpp文件中提供其他定义。 You can only have one definition. 您只能有一个定义。

There are two ways to fix this. 有两种方法可以解决此问题。

1) implicitly inline definition of the constructor in the class definition. 1)在类定义中隐式内联定义构造函数。 Modify your existing header code to call the appropriate QMainWindow constructor in the initialization list. 修改现有的标头代码,以在初始化列表中调用适当的QMainWindow构造函数。 And remove the constructor definition from the .cpp file: 并从.cpp文件中删除构造函数定义:

// in test.h
test(QWidget* parent = 0) : QMainWindow(parent), control(device, 0) 
{
  ui.setupUi(this);    
  device.start();  
  control.start();
}

2) Declare the constructor in the header, and define it in the .cpp : 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();
}

Either of these solutions should work. 这些解决方案都应该起作用。 What you can't have is a combination of the two, which is what your code sample exhibits. 您不能拥有的是两者的结合,这就是您的代码示例所展示的。

You may also need a definition of test::~test() . 您可能还需要定义test::~test() In your code sample you only show a declaration. 在您的代码示例中,您仅显示一个声明。

You have two major issues ongoing: 您有两个主要问题正在进行中:

1) You should probably write this constructor leaving the definition for the constructor in the header behind. 1)您可能应该编写此构造函数,而在标头中保留该构造函数的定义。

You ran into the trap of two definitions provided, one in the header, and the other in the source file. 您遇到了提供的两个定义的陷阱,一个定义在标头中,另一个定义在源文件中。

You either need to have the declaration in the header, and definition in the source, or the declaration and definition in the header. 您要么需要在标头中声明,在源中定义,要么在标头中声明和定义。 You cannot have one declaration and two definitions distributed in the header and source files! 您不能在头文件和源文件中分配一个声明和两个定义!

You had the empty definition in the header by {} and the other definition with { .... } in the source file. 您在头文件中用{}定义了空定义,在源文件中用{ .... }定义了另一个定义。 How would the compiler know which one you really wanted? 编译器如何知道您真正想要哪一个? It cannot reliably... even if it picks up either version. 它不能可靠地...即使选择任何一个版本。

2) Also, your testControl destructor seems to be private. 2)另外,您的testControl析构函数似乎是私有的。 That is not a good idea. 那不是一个好主意。 Change that destructor to public! 将该析构函数更改为公共!

test.h 测试

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_test.h"
#include "testDevice.h"
#include "testControl.h"

class test : public QMainWindow
{
    Q_OBJECT

public:
    // Only declare, do not define it here
    // Also, for completeness: you should use the "explicit" keyword in here.
    explicit test(QWidget *parent = 0);
    ~test();

private:
    Ui::testClass ui;
    testDevice device;
    testControl control;
};

test.cpp 测试文件

#include "test.h"

test::test(QWidget *parent = 0)
    : QMainWindow(parent)
    , control(device,0)
{
    ui.setupUi(this);    
    device.start();  
    control.start();
}

test::~test()
{
}

testControl.h testControl.h

#pragma once

#include <QThread>
#include "testDevice.h"

class testControl : public QThread 
{
    Q_OBJECT

public:
    testControl(testDevice& device, QObject *parent = 0);
    // Change this to being _public_ rather than _private_ as in your code.
    ~testControl(void);

protected:
    void run(void);

private:
    testDevice device;
};

When a class is constructed, all the data members are constructed as well. 当构造一个类时,所有数据成员也被构造。 If there is a default constructor for a particular data member, that one is executed. 如果特定数据成员有默认构造函数,则执行该构造函数。

If you need to execute a specific non-default constructor, you must use the member initialisation list in order to construct that particular data member. 如果需要执行特定的非默认构造函数,则必须使用成员初始化列表来构造该特定数据成员。

I suspect that your problem is with either your 我怀疑您的问题出在您的

testDevice device;
testControl control;

data members, or both. 数据成员,或两者都有。

What are the available constructors for both these classes? 这两个类都有哪些可用的构造函数? If there are no default constructors available, you must initialise them in the members' initialisation list, supplying appropriate parameter arguments. 如果没有可用的默认构造函数,则必须在成员的初始化列表中对其进行初始化,并提供适当的参数参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM