简体   繁体   中英

Object Composition C++: no matching function for call

I am trying to create an object named . 的对象。 This object is composed of object, object, Object. 对象, 对象, 对象组成。 The last three classes are working perfectly separated. But when I try to glue everything together in the luminary class I got these message:

luminary.cpp:11:112: error: no matching function for call to 'Thermometer::Thermometer()' luminary.cpp:11:112: error: no matching function for call to 'Memory::Memory()'
luminary.cpp:11:112: error: no matching function for call to 'Led::Led()'

Code for header file of luminary class:

class Luminary{

public:
    //Constructor
    Luminary(Led led,Thermometer thermometer,Memory memory);

    //Atributes
    Led _led;
    Thermometer _thermometer;
    Memory _memory;
}

Code for cpp file:

#include "luminary.h"
#include "Led.h"
#include "Thermometer.h"
#include "Memory.h"


//Constructor
Luminary::Luminary(Led led,Thermometer thermometer,Memory memory){

    _memory = memory;
    _thermometer = thermometer;
    _led = led;

}

Why do I get these messages?

According to your source, Led , Thermometer , Memory have to be default constructible, means they should have a default constructor, but they haven't.

You could use member initializer list here:

Luminary::Luminary(Led led,Thermometer thermometer,Memory memory) 
    : _led(led), _thermometer(thermometer), _memory(memory) {}

Here is a discussion about why in moust cases, you should use initialization lists rather than assignment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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