1

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)

3
  • Please post more code. Where are you trying to create an object of type deviceC? Commented Apr 4, 2014 at 2:34
  • Can we see the constructors of wayPoint? Commented Apr 4, 2014 at 2:36
  • thaks everyone. it is working after add an initializer list like @songyuanyao said. Commented Apr 4, 2014 at 2:47

2 Answers 2

3

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) {}
};
Sign up to request clarification or add additional context in comments.

4 Comments

it is working now. I had to initialize the other variable "current" too. thanks..
@kasunbdn I've edited my answer to give some more suggestion, hope it helps.
yeah it was very helpful. i was coding in java and now trying to do with c++. but couldn't get "you don't need init the devA and devB inside the constructor just with the default constructor" so when we initialize devA and B?
@kasunbdn They will be initialized by default constructor when you write private: deviceA devA; deviceA devB;. You don't need do anything else such as deviceA devA = new deviceA(); like Java. If you want them initialized by their other constructor with some parameters, you can use the initializer list to change the behaviour.
1

Missed a bracket.

class deviceC{

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

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

1 Comment

sorry. i was a mistake when i copied the code here. thakns. it is working now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.