cannot find default constructor to initialize member in cpp

后端 未结 2 712
离开以前
离开以前 2020-12-22 04:10

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         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-22 04:59

    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) {}
    };
    

提交回复
热议问题