请帮我解决一下这个。我试图修复两个小时。
这是我的代码。

class deviceC {

private:
    deviceA devA;
    deviceB devB;
    wayPoint destination,current;

public:
    deviceC(wayPoint destination1){
        destination=destination1;
        devA=deviceA();
        devB=deviceB();
    }
};

这是错误:

最佳答案

您在构造函数中需要一个初始化程序列表,因为类型为destination的成员currentwayPoint没有默认的构造函数。

class deviceC {
public:
    deviceC(wayPoint destination1) : destination(destination1) {
        devA=deviceA();
        devB=deviceB();
    }
};

而且IMO,您不需要仅使用默认构造函数来初始化构造函数内部的devAdevB,它们只需在调用其默认构造函数之后调用operator=。这是我的建议:
class deviceC {
private:
    deviceA devA;
    deviceB devB;
    wayPoint destination, current;
public:
    deviceC(const wayPoint& destination1, const wayPoint& current1) : destination(destination1), current(current1) {}
};

关于c++ - 找不到默认的构造函数来初始化cpp中的成员,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22852436/

10-10 08:07