0

I started out with a piece of code that both declares and initialises two objects from 3rd party libraries in one line (see below). That worked just fine.

#include <OneWire.h>
#include <DallasTemperature.h>

const int gpioPin = 0;
OneWire oneWire(gpioPin);
DallasTemperature sensors(&oneWire);

I then wanted to wrap it in a class and initialise oneWire and sensors when the class is initialised. So I came up with this:

#include <OneWire.h>
#include <DallasTemperature.h>

class TemperatureSensor
{
private:
  OneWire oneWire;
  DallasTemperature sensors;

public:
  TemperatureSensor(int gpioPin);
};

TemperatureSensor::TemperatureSensor(int gpioPin)
{
  oneWire(gpioPin);
  sensors(&oneWire);
}

Which throws the error call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type. So I guess I can't initialise an object like that when the declaration is already made. Any help is much appreciated.

Awe
  • 1
  • 2
    related/dupe: https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor – NathanOliver Feb 09 '22 at 21:52
  • 1
    You can only call the constructor directly, if you're using a initializer list (`TemperatureSensor::TemperatureSensor(int gpioPin) : oneWire(gpioPin), sensors(&oneWire) {}`). Once the constructor body is entered, all member variables of class type have been constructed (in this case using the default constructor. I hope there's a move or copy assignment opertor for those types, so that you can do `TemperatureSensor::TemperatureSensor(int gpioPin) { oneWire = OneWire(gpioPin); sensors = DallasTemperature(&oneWire); }`... – fabian Feb 09 '22 at 21:53
  • 1
    `sensors(&oneWire);` means `sensor.operator()(&oneWire);`. It does not mean construct `sensor`. – user4581301 Feb 09 '22 at 21:53
  • Thanks, the initialiser list got me going in the right direction! – Awe Feb 10 '22 at 22:01

0 Answers0