I'm writing a library for my project, which interfaces sensors used and structures them in different systems.
But I'm such a noob at C++. The compiler is throwing this error:
libraries/cec-core/cec-core.cpp: In member function 'void Habitaculo::Update()':
libraries/cec-core/cec-core.cpp:15:8: error: expected unqualified-id before '.' token
_Sluz.Update();
This is the header of that class:
class Habitaculo
{
public:
Habitaculo(int refrate, int luzpin, int dhtpin);
//Values from Indoor Sensors
int temp;
int hum;
int luz;
//Update Methods
void Update(); //Updates sensors value
void UpdateReg(); //writes to SD or enabled output method
private:
//Clock Managing
long _PreviousMillis;
unsigned long _CurrentMillis;
int _RefreshRate;
//Sensor instances
class _Sluz;
class _SDHT;
};
This are the member functions affected:
Habitaculo::Habitaculo(int refrate, int luzpin, int dhtpin){
//Clock management
_RefreshRate = refrate;
_PreviousMillis = 0;
//Systems to initialize
LDR _Sluz(luzpin); //analogpin
DHT11 _SDHT(dhtpin); //buspin
}
void Habitaculo::Update(){
_CurrentMillis = millis();
if (_CurrentMillis-_PreviousMillis > _RefreshRate){
_Sluz.Update(); //unqualified-id before '.' token
_SDHT.Update(); //unqualified-id before '.' token
temp = _SDHT.temp; //expected primary-expression before '.'
hum = _SDHT.hum; //expected primary-expression before '.'
luz = _Sluz.value; //expected primary-expression before '.'
_PreviousMillis = _CurrentMillis;
}
}
The error is thrown in every step of the Update
method. But I can copy that piece of code right in the Habitaculo
constructor and it works well. So I assume I'm missing something basic about language syntax, because I'm really new to all this scene.
Every class involved in this (LDR, DHT11) works fine. It's when I try to put all this together into a new class - inside an unified Update method - that everything crashes.
2 Answers 2
From what I guess in your code, here is how you want your class declared:
class Habitaculo
{
public:
Habitaculo(int refrate, int luzpin, int dhtpin);
//Values from Indoor Sensors
int temp;
int hum;
int luz;
//Update Methods
void Update(); //Updates sensors value
void UpdateReg(); //writes to SD or enabled output method
private:
//Clock Managing
long _PreviousMillis;
unsigned long _CurrentMillis;
int _RefreshRate;
//Sensor instances
LDR _Sluz;
DHT11 _SDHT;
};
Of course, in your header file, you will have to #include
the proper header files for DHT11
and LDR
classes.
Then you will need to change your constructor as follows:
Habitaculo::Habitaculo(int refrate, int luzpin, int dhtpin)
:_Sluz(luzpin), _SDHT(dhtpin)
{
//Clock management
_RefreshRate = refrate;
_PreviousMillis = 0;
}
Note the :_Sluz(luzpin), _SDHT(dhtpin)
line before the constructor body, in C++, this is how members of your class that are instances of another class must be instantiated.
Actually, you could even empty the whole cosntructor body by also initializing _RefreshRate
and _PreviousMillis
in the same manner:
Habitaculo::Habitaculo(int refrate, int luzpin, int dhtpin)
:_PreviousMillis(0), _RefreshRate(refrate), _Sluz(luzpin), _SDHT(dhtpin) {}
which is a bit more idiomatic C++.
-
Seems like you hit the nail with what I'm trying to achieve. Is any problem if LDR and DHT11 are defined in the same header that Habitáculo?David P.– David P.2016年11月13日 16:59:14 +00:00Commented Nov 13, 2016 at 16:59
-
No there is no problem here, as long as they are defined first, before
Habitaculo
class.jfpoilpret– jfpoilpret2016年11月13日 17:00:11 +00:00Commented Nov 13, 2016 at 17:00 -
Done. So many thanks for the answer. Definitely I have to dig more in c++ and it's syntax. Thanks again!David P.– David P.2016年11月13日 17:08:07 +00:00Commented Nov 13, 2016 at 17:08
class _Sluz;
class _SDHT;
You're forward declared two classes but I see nothing about what those classes are. You then to and use those class declarations as if they were class instances.
I think you are lacking a bit of basic syntax knowledge there. You may have meant:
Sluz _Sluz;
SDHT _SDHT;
Though not knowing what those classes are meant to be or how they are even defined I cannot know really what you think you're meaning.
-
I assume. It's basic knowledge lack. What I want to do is put two instances of LDR and DHT11 (defined and working) inside Habitáculo instance with names sluz and sdhtDavid P.– David P.2016年11月13日 16:31:00 +00:00Commented Nov 13, 2016 at 16:31
-
Are they instances that are defined outside the class (in your main sketch), or do you want to create two new, distinct, instances within your class?Majenko– Majenko2016年11月13日 16:32:02 +00:00Commented Nov 13, 2016 at 16:32
-
I want to create them when I create an Habitáculo instanceDavid P.– David P.2016年11月13日 16:34:15 +00:00Commented Nov 13, 2016 at 16:34
-
So the "class" needs to be the actual class name that you are instantiating then. Like you would if you were making them in your sketch, but without any constructor arguments (those have to be in an initialization list for your constructor).Majenko– Majenko2016年11月13日 16:35:17 +00:00Commented Nov 13, 2016 at 16:35