what am i doing wrong?
class Sensor
{
// Class Member Variables
// These are initialized at startup
int read; // sensor value
int value; // calculated value
int pin_;
// Constructor - creates sensor
// and initializes the member variables and state
public:
Sensor(int pin)
{
pin_ = pin;
}
int Update()
{
int read = analogRead(pin_);
int value = read * (500 / 1023.0); // calculated value
return 1; // test return
}
};
Sensor hi(A0);
void setup()
{
Serial.begin(9600);
}
void loop()
{ int hi = 0;
hi = hi.Update();
// Serial.println(hi);
}
I get Error:
request for member 'Update' in 'hi', which is of non-class type 'int'
I did declare it...
Um... You declared another hi
locally. And you declared it is an int
.
int hi = 0;
The local hi
"hides" the outer hi
. So you are trying to call Update()
on an object of type int
, which makes no sense. Which is exactly what the compiler is telling you.
Don't give your variables identical/conflicting names.
In this case you can save the day by using a scope resolution operator, i.e. by calling it as
hi = ::hi.Update();
(and use ::hi
every time you want to refer to the "hidden" outer hi
).
This will work. But it still doesn't make it a good idea.
LMAO
Late night haha
Thank you!!
class Sensor
{
// Class Member Variables
// These are initialized at startup
int read; // sensor value
int value; // calculated value
int pin_;
// Constructor - creates sensor
// and initializes the member variables and state
public:
Sensor(int pin)
{
pin_ = pin;
}
int Update()
{
int read = analogRead(pin_);
int value = read * (500 / 1023.0); // calculated value
return value; // test return
}
};
Sensor hi(A0);
void setup()
{
Serial.begin(9600);
}
void loop()
{ int lo = 0;
lo = hi.Update();
Serial.println(lo);
}