Use of library and class in another library and class

So, I am trying to use OneWire class in a new class called DS18B20.

This are the important parts from .h file:

#include "OneWire.h"

#ifndef nedsensor_h
#define nedsensor_h
#endif

#include "Arduino.h"


class DS18B20
{
  private:
  OneWire _ds(int);
}

And the .cpp file:

DS18B20::DS18B20(int pin = 2){
    OneWire _ds(pin);
    _ds.reset();
}

I have other functions too but when I use functions like _ds.reset() in them I get the following error:

Documents\Arduino\libraries\nedsensor\nedsensor.cpp:18:13: error: '((DS18B20*)this)->DS18B20::_ds' does not have class type

             _ds.reset();

but in the constructor it works doesn't get an error so I guess the problem is that it does not save data in the class.

Well in the constructor you declare a local variable of type OneWire that has the same name of something you tried to create as the instance variable, with a very limited scope and lifetime..

What you need is a instance variable which is a pointer to a an instance of the OneWire class in your .h and create (instantiate) that in the class constructor.

Makes sense?

(But then with OneWire it might be a special case in the way that works - do you really need that in your class ?)

Yes, thanks, I done it!

I have tried that before but I made a little mistake.

For those who are interested in how I fixed it (after J-M-L advice) here is the updated code:

class DS18B20
{
  private:
  OneWire* _ds;
}

and

DS18B20::DS18B20(int pin = 2){
    _ds = new OneWire(pin);
}

Good.

You could simplify it a bit by doing

_ds = new OneWire(pin);

In the constructor body code

Alternatively (better) you could use DS18B20::DS18B20(int pin = 2): _ds(new OneWire(pin)) {}
as an initializer list is usually preferred for the constructor

@J-M-L ok, thanks!