无法找到默认构造函数在cpp中初始化成员 [英] cannot find default constructor to initialize member in cpp

查看:161
本文介绍了无法找到默认构造函数在cpp中初始化成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  class deviceC {

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

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

这是错误:


无法找到默认构造函数来初始化成员
'deviceC :: destination'in function deviseC :: destination(wayPoint)



目标和当前

code>类型 wayPoint 没有默认构造函数。

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

并且IMO,你不需要init devA devB 在构造函数中使用默认构造函数,他们只是调用 operator = 称为。这是我的建议:

  class deviceC {
private:
deviceA devA;
deviceB devB;
wayPoint destination,current;
public:
deviceC(const wayPoint& destination1,const wayPoint& current1):destination(destination1),current(current1){}
};


Please help me with this. I was trying to fix this for two hours. This is my code.

class deviceC {

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

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

This is the error:

cannot find default constructor to initialize member 'deviceC::destination' in function deviseC::destination(wayPoint)

解决方案

You need an initializer list in your constructor, because member destination and current with type wayPoint does not has a default constructor.

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

And IMO, you don't need init the devA and devB inside the constructor just with the default constructor, they just call the operator= after their default constructor called. Here's my suggestion:

class deviceC {
private:
    deviceA devA;
    deviceB devB;
    wayPoint destination, current;
public: 
    deviceC(const wayPoint& destination1, const wayPoint& current1) : destination(destination1), current(current1) {}
};

这篇关于无法找到默认构造函数在cpp中初始化成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆