2

I want to pass a lambda function as a parameter to another function. It does not work with a member function. In this case Max6675Sensor::update().

Error Message: no matching function for call to 'Max6675Sensor::setInterval(const char [7], int, Max6675Sensor::setup()::)

The goal is to call Max6675Sensor::update() every 2000ms.

I can't use std::function -> "std::function' has not been declared"

thanks for your help!

max6675_component.cpp

class Max6675Sensor : public Component

max6675_component.cpp

void Max6675Sensor::setup()
{
    thermocouple.begin(this->clkPin, this->csPin, this->soPin);

    // does not work
    // no matching function for call to 'Max6675Sensor::setInterval(const char [7], int, Max6675Sensor::setup()::<lambda()>)'
    this->setInterval("update", 2000, [this]() {
        this->update();
    });

    // works
    this->setInterval("update", 2000, []() {
        Serial.println("Test");
    });
}

void Max6675Sensor::update()
{
    this->value = this->thermocouple.readCelsius();
    Logger->debug("sensor.max6675", "Neuer Wert: %f", this->value);
}

component.cpp

// Header
void setInterval(const std::string &name, uint32_t interval, void (*f)());
//
void Component::setInterval(const std::string &name, uint32_t interval, void (*f)())
{
    int offset = 0;
    if (interval != 0)
    {
        offset = (random(65535) % interval) / 2;
    }
    this->cancelInterval(name);
    struct TimeFunction function { name, TimeFunction::INTERVAL, interval, millis() - interval - offset, f, false };
    this->timeFunctions.push_back(function);
}
daniels
  • 21
  • 2
  • 2
    Didn't you forget `#include ` for `std::function`? – acade Sep 15 '18 at 12:00
  • i use this library: https://github.com/maniacbug/StandardCplusplus and i include but it does not work. Do you have any tipps on why? i would like to use std::function. std::vector works just fine although not all functions are implementet such as shrink_to_fit – daniels Sep 15 '18 at 12:26

0 Answers0