ERROR: expected initializer before 'dht'

Hello.
I'm trying to make my first library, but I have some problem.
I have my sketch working, but when I add my library (UbidotsWeather.h) an error appeared but I don't know what is the problem.
Any help?
(I upload it as file because it is too long for the forum)

UbidotsWeather.cpp (592 Bytes)

UbidotsWeather.h (273 Bytes)

WeatherStationMaster.ino (9.44 KB)

I have my sketch working, but when I add my library (UbidotsWeather.h) an error appeared but I don't know what is the problem.

If you want help with an error, don't you suppose that you should share the error message?

	Ubidots client(_token);

Creating a local variable that immediately goes out of scope seems pointless.

Pissing away resources on the String class seems pointless. Everything you do with them can be done with strings.

If you want help with an error, don't you suppose that you should share the error message?

Sorry, I wrote it in the title. Here is the error anyway

Arduino: 1.6.11 (Mac OS X), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 115200, 4M (3M SPIFFS)"

WeatherStationMaster:22: error: expected initializer before 'dht'
 DHT dht(DHTPIN, DHTTYPE);
     ^
/Users/Mattia/Desktop/Arduino/WeatherStation/WeatherStationMaster/WeatherStationMaster.ino: In function 'void setup()':
WeatherStationMaster:164: error: 'dht' was not declared in this scope
   dht.begin();
   ^
/Users/Mattia/Desktop/Arduino/WeatherStation/WeatherStationMaster/WeatherStationMaster.ino: In function 'void drawHeaderOverlay(OLEDDisplay*, OLEDDisplayUiState*)':
WeatherStationMaster:298: error: 'dht' was not declared in this scope
   String temp = String(dht.readTemperature()) + "°C";
                        ^
/Users/Mattia/Desktop/Arduino/WeatherStation/WeatherStationMaster/WeatherStationMaster.ino: In function 'void ubidotsDataUpload()':
WeatherStationMaster:310: error: 'dht' was not declared in this scope
   float temp = dht.readTemperature();
                ^
exit status 1
expected initializer before 'dht'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Creating a local variable that immediately goes out of scope seems pointless.

So delete _token = token; and use Ubidots client(token);?

Pissing away resources on the String class seems pointless. Everything you do with them can be done with strings.

Sorry but I don't understand what it means.
And what can cause the error?

A link to the DHT library you are using would be in order. There are several DHT libraries, some containing a class called dht while others contain a class called DHT.

I use Ubidots client(_token); because I need to start another library.

How does that help, in the UbidotsWeather constructor, when that instance immediately goes out of scope?

DHT library properties:

name=DHT sensor library
version=1.3.0
author=Adafruit
maintainer=Adafruit <info@adafruit.com>
sentence=Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors
paragraph=Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors
category=Sensors
url=https://github.com/adafruit/DHT-sensor-library
architectures=*

Wheather library:

name=ESP8266 Weather Station
version=1.1.1
author=Daniel Eichhorn
maintainer=Daniel Eichhorn <squix78@gmail.com>
sentence=ESP8266 based internet connected Weather Station
paragraph=ESP8266 based internet connected Weather Station
category=Display
url=https://github.com/squix78/esp8266-weather-station
architectures=esp8266

Oled library:

name=ESP8266 Oled Driver for SSD1306 display
version=3.2.3
author=Daniel Eichhorn, Fabrice Weinberg
maintainer=Daniel Eichhorn <squix78@gmail.com>
sentence=A I2C display driver for SSD1306 oled displays connected to an ESP8266
paragraph=A I2C display driver for SSD1306 oled displays connected to an ESP8266
category=Display
url=https://github.com/squix78/esp8266-oled-ssd1306
architectures=esp8266

The DHT library comes with examples that do not use 147 other libraries. Do the examples compile and link for you?

The example works well. But also the sketch if I remove my library works well

Eternyt:
The example works well. But also the sketch if I remove my library works well

I can't imagine why adding your library has any impact on whether or not the DHT class is defined.

I also noticed this, though:

#ifndef Morse_h
#define Morse_h
#include <ESP8266WiFi.h>
#include "UbidotsMicroESP8266.h"

class UbiUbi {

The include guard is supposed to prevent you from including you class definition twice. That means that the include guard (the first two lines) need to use YOUR class name, not some other class name.

I retry to comment all the things related to my library and compile successfully.

The include guard is supposed to prevent you from including you class definition twice. That means that the include guard (the first two lines) need to use YOUR class name, not some other class name.

Thanks, I now put UbidotsWeather_H, it's correct?

Thanks, I now put UbidotsWeather_H, it's correct?

No. Your class name is not UbidotsWeather.

UbiUbi_H?

Also, I take the library example of the DHT library (compiling perfectly), I just add #include "UbidotsWeather.h" and then compare the SAME error...

UbiUbi_H?

Yes.

I just add #include "UbidotsWeather.h" and then compare the SAME error...

Remove the two #include statements from UbidotsWeather.h. Does the error go away? Add them, one at a time, to the sketch. Which one causes the problem?

Only compile if I remove the class UbiUbi. Not just what's inside it, the ENTIRE class.

I see the problem. Take a look at the Morse code example again. That appears to be the starting point for defining your class. Compare the class statement, it's opening and closing curly braces AND WHAT FOLLOWS THEM to those in your class.

MANY THANKS!!!
I add the semicolon and works fine!

I have another question :D.
In the .cpp file, I need to instance a library, Ubidots client(token);. But if I declare it in the constructor function (UbiUbi::UbiUbi) it gives me an error. Only if I put it in the update function works correctly. Is there a way to declare it once in the constructor function and not every time I call back the update one?

I add the semicolon and works fine!

Took me a while to spot that. Glad you got the clues.

Is there a way to declare it once in the constructor function and not every time I call back the update one?

No IN the constructor. You declare client as a private instance of the class, and create it at the same time as you create the UbiUbi instance.

private:
	char* _token;
        Ubidots client;


UbiUbi::UbiUbi(char* token, char* temp, char* hum) : client(token)
{ // Down here where it belongs

No IN the constructor. You declare client as a private instance of the class and create it at the same time as you create the UbiUbi instance.

Works well also this!

I have a problem now...The library must return the two String when I need it, but it returns nothing. But I'm sure that the data is present in the variable because I put a print in the library, after getting the two variable, and the values are correct. Is when I try to use it in the main sketch that seems empty.

This is the actual .cpp code:

#include "UbidotsWeather.h"

UbiUbi::UbiUbi(char* token, char* temp, char* hum) : client(token) {
	_temp = temp;
	_hum = hum;
}

void UbiUbi::update() {
	String _ttt = String(client.getValue(_temp)); // Get the outdoor temperature from Ubidots cloud
    Serial.println("Variabile temp presa");
    Serial.println(_ttt); //return the correct value
    String _hhh = String(client.getValue(_hum)); // Get the outdoor humidity from Ubidots cloud
    Serial.println("Variabile hum presa");
    Serial.println(_hhh); //return the correct value
}

String UbiUbi::ttt() {
	return _ttt;
}

String UbiUbi::hhh() {
	return _hhh;
}

And I use this lines of code in the sketch:

ttt = ubi.ttt();
Serial.print(ttt);

Where is the error now?

Many thanks!!!!!

Where is the error now?

You have class fields:

private:
	char* _token;
	char* _temp;
	char* _hum;
	String _ttt;
	String _hhh;

You have a method in the class:

void UbiUbi::update() {
	String _ttt = String(client.getValue(_temp)); // Get the outdoor temperature from Ubidots cloud
    Serial.println("Variabile temp presa");
    Serial.println(_ttt); //return the correct value
    String _hhh = String(client.getValue(_hum)); // Get the outdoor humidity from Ubidots cloud
    Serial.println("Variabile hum presa");
    Serial.println(_hhh); //return the correct value

Which class fields is that method valuing? None, because you have variables that are local to the function that hide the class fields.

Then, you call ubi.ttt() which doesn't update the class field before returning the contents of the field. That means that you must have called (and fixed) ubi.update() before calling ttt() or hhh().

Many, MANY thanks!
I remove the String before the variable and finally all works fine!

    _ttt = String(client.getValue(_temp)); // Get the outdoor temperature from Ubidots cloud
    Serial.println("Variabile temp presa");
    Serial.println(_ttt);
    _hhh = String(client.getValue(_hum)); // Get the outdoor humidity from Ubidots cloud
    Serial.println("Variabile hum presa");
    Serial.println(_hhh);

Many thanks for your time!