-1

I have this code: sensor.h:

template<class T>
class Sensor {
    public:
        uint8_t address;
        T data;
        virtual void collectData() = 0;
        Sensor(uint8_t address);
};

class TemperatureSensor: public Sensor<float> {
    void collectData();
};

sensor.cpp:

template<typename T>
Sensor<T>::Sensor(uint8_t address) {
    this->address = address;
}

void TemperatureSensor::collectData() {
    //some code for collecitng data
}

main function:

    TemperatureSensor sensor;
    Serial.printf("%d", sensor.address);

Error:

src\sensor.cpp: In function 'void test()':
src\sensor.cpp:11:23: error: use of deleted function 'TemperatureSensor::TemperatureSensor()'
   11 |     TemperatureSensor sensor;
      |                       ^~~~~~
In file included from src\sensor.cpp:1:
src/sensor.h:14:7: note: 'TemperatureSensor::TemperatureSensor()' is implicitly deleted because the default definition would be ill-formed:       
   14 | class TemperatureSensor: public Sensor<float> {
      |       ^~~~~~~~~~~~~~~~~
src/sensor.h:14:7: error: no matching function for call to 'Sensor<float>::Sensor()'
src/sensor.h:11:9: note: candidate: 'Sensor<T>::Sensor(uint8_t) [with T = float; uint8_t = unsigned char]'
   11 |         Sensor(uint8_t address);
      |         ^~~~~~
src/sensor.h:11:9: note:   candidate expects 1 argument, 0 provided
src/sensor.h:6:7: note: candidate: 'constexpr Sensor<float>::Sensor(const Sensor<float>&)'
    6 | class Sensor {
      |       ^~~~~~
src/sensor.h:6:7: note:   candidate expects 1 argument, 0 provided
src/sensor.h:6:7: note: candidate: 'constexpr Sensor<float>::Sensor(Sensor<float>&&)'
src/sensor.h:6:7: note:   candidate expects 1 argument, 0 provided
*** [.pio\build\nodemcuv2\src\sensor.cpp.o] Error 1

I want to have multiplte options of same base class(Sensor class) and extend it(I think this is rigth name). I cant create new instance of TemperatureSensor, from error i asume that i need to pass reference of Sensor, but I cant create new Sensor, beacuse it is virtual. Also this is not expected by me behavior. I want to create TemperatureSensor using constructor defined by Sensor ex: TemperatureSensor sensor(0xbeef/*address*/)

wojtess
  • 72
  • 3

1 Answers1

2

I want to create TemperatureSensor using constructor defined by Sensor ex: TemperatureSensor sensor(0xbeef/*address*/)

If you want to use base class constructor directly, you can use using

class TemperatureSensor: public Sensor<float> {
    using Sensor::Sensor;
    void collectData();
};
apple apple
  • 10,292
  • 2
  • 16
  • 36