Hello everybody!
If have a little issue with classes as I'm not so familiar with OOP, i fear.
I'd like to create a class called "TempSensor", which is using the OneWire library. The header of my class looks like this:
#include <OneWire.h>
class TempSensor
{
public:
TempSensor(byte);
byte Init(void);
double GetTemperature(void);
byte GetStatus(void);
private:
** OneWire TS;**
byte Address[8]; //The sensor address, needed to access the temperature sensor via OneWire bus
byte Data[8]; //Contains the raw data read from the sensor
int16_t Raw; //The raw temperature value
double CurrentTemperature; //This is the global variable for the current temperature
unsigned long Value_time; //the last time a value conversion has been triggered is stores here
byte Status; //returns the current sensor status
double Conversion_Time; //The conversin time is waited until a new sensor value is requested, shall be > 750ms for the DS sensor types
byte i; //Counting variable
byte Pin_Sensor; //The pin at which the sensor is connected
};
Well I thought I can simply define a class of type "OneWire" within my class. However when it comes to the constructor of my class:
TempSensor::TempSensor(byte set_Pin_Sensor){
** OneWire TS(Pin_Sensor); //At first we create our one wire class**
Conversion_Time = 800; //The conversin time is waited until a new sensor value is requested, shall be > 750ms for the DS sensor types
Pin_Sensor = set_Pin_Sensor; //We save the sensor pin
Init();
}
There is an error 
So how do I create a class OneWire within the constructor of my class TempSensor? The special point here is also that I need to hand over the Pin_Sensor variable from the constructor of my class to the constructor of the OneWire class and I have to idea how to convince the compiler about this 
Hopefully somebody here is able to help me 
Best regards
Daniel
You need to read up on initialization lists. These are used to initialize your variables.
TempSensor::TempSensor(byte set_Pin_Sensor) : TS(Pin_Sensor){
//...
}
Yeah!
That's it! Now it compiles! 
I tried to find a solution in some c++ tutorials, but I didn't stumble over initialization lists yet.
Thanks a lot!
Best regards
Daniel
Hey!
It's me again! 
May I ask if you have ever used such an initialization list in an Arduino Sketch?
I tried it and it compiles without errors. So far so good.
The problem is: When I upload the sketch via USB, my Arduino Micro disappears from the USB, the loop lamp is off and that was it.
I can even reproduce it with a no-name china clone of the arduino micro. Same behaviour..you flash the sketch and that is the last thing you hear from the arduino.
Well, i own an ISP programmer now! Hey! After burning the bootloader again via ISP, the processor is fine again and reachable via USB, at least!
Well to cross check I reverted my sketch back to the version without the initialization list (no other changes). Uploaded, works as before, no problem.
However I have the impression this is a bigger issue with the compiler here, because obviously using this initialization stuff leads to a sketch upload, that corrupts the boatloader section. However this is even possible...
Do you have any ideas about this?
Best regards
Daniel
danielschlingmeier:
TempSensor::TempSensor(byte set_Pin_Sensor){
** OneWire TS(Pin_Sensor); //At first we create our one wire class**
Conversion_Time = 800; //The conversin time is waited until a new sensor value is requested, shall be > 750ms for the DS sensor types
Pin_Sensor = set_Pin_Sensor; //We save the sensor pin
Init();
}
The syntax is:
TempSensor::TempSensor(byte set_Pin_Sensor) : TS(set_Pin_Sensor) {
Conversion_Time = 800; //The conversin time is waited until a new sensor value is requested, shall be > 750ms for the DS sensor types
Pin_Sensor = set_Pin_Sensor; //We save the sensor pin
Init();
}
This is the same syntax as is used for calling superclass constructors.
You can also use this syntax for setting const member variables. I like to do this for things that wont change over the lifetime if the object, like pin assignments:
const byte Pin_Sensor;
const uint32_t Conversion_Time;
TempSensor::TempSensor(byte set_Pin_Sensor) :
TS(set_Pin_Sensor),
Pin_Sensor(set_Pin_Sensor),
Conversion_Time(800)
{
Init();
}
If your init() function does anything with hardware, then it should not be called in the constructor, because constructors are executed before the chip is set up. Instead, call your init() in setup().
pYro_65:
You need to read up on initialization lists. These are used to initialize your variables.
TempSensor::TempSensor(byte set_Pin_Sensor) : TS(Pin_Sensor){
//...
}
This will compile but it won't work - it will set the TS pin to zero.
Do you really need a separate Pin_Sensor member variable? After all - the only thing that needs to know about it is the OneWire object.
Well thanks a lot for your support! It's really great, that there are people here dealing with my problem! Thanks so much! 
However I guess I see my failure now, I hope! I simply forgot to the "set_..." before Pin_Sensor. How stupid... 
If it initializes as I coded it "Pin_Sensor" is zero, when I call the constructor, which can lead to serious trouble I guess! :o
However it shall work like this, right?
TempSensor::TempSensor(byte set_Pin_Sensor) : TS(set_Pin_Sensor){
//...
}
Away from this I'm a bit confused with this sentence:
If your init() function does anything with hardware, then it should not be called in the constructor, because constructors are executed before the chip is set up. Instead, call your init() in setup().
Well if you use OneWire library, you hand over the pin via the constructor and yes, this is done before Setup(), of course, and yes, it does "something with hardware" I guess. I don't know the library's internals. However it works like this. The only thing I try to do here is creating a class of OneWire properly within my own class.
However I guess we are on a very good way here. I'll try to revert the reversion of the reversion in Github tomorrow and correct the little "set_.." issue. I'm quite hopeful this will work.

(If not I have a ISP programmer now, so not much can go wrong anymore I guess :D)
danielschlingmeier:
Well if you use OneWire library, you hand over the pin via the constructor and yes, this is done before Setup(), of course, and yes, it does "something with hardware" I guess.
Mainly, I'm talking about pinMode. if OneWire sets the pinmode, then that's a problem - but I'd be v. surprised if it did.
I like to set pinMode in my classes, because that means I don't have to have keep track of which pins are which in two separate places. But to do this, I always have a separate setup() function in the class.
If your are interested in this kind of thing, you might be interested in my webpage Arduino The OO Way. I do what you are doing and also use a few other techniques (inheritance, composition by reference).