I've tried to build a library (file.cpp + file.h) to Arduino but I got this error:
In file included from EXAMPLE_MPL3115A2.ino:2: /Applications/Arduino.app/Contents/Resources/Java/libraries/MPL3115A2/MPL3115A2.h:8: error: ISO C++ forbids declaration of 'getPressure' with no type EXAMPLE_MPL3115A2.ino: In function 'void loop()': EXAMPLE_MPL3115A2:18: error: expected unqualified-id before '.' token
Can you explain me where is the error?? (I don't know very well C++)
Here are my MPL3115A2.h
#ifndef MPL3115A2_h
#define MPL3115A2_h
#include "Arduino.h"
class MPL3115A2{
public:
getPressure();
private:
int m_i2c_address;
byte _p_msb, _p_csb, _plsb;
byte _tempp, _tempa, _tempt, _decimalPress, _decimalAlt, _decimalTemp;
unsigned long _ptot;
};
#endif
my MPL3115A2.cpp
#include "Arduino.h"
#include "MPL3115A2.h"
#include "I2C.h"
MPL3115A2::MPL3115A2()
{
m_i2c_address = 0x60;
p_msb = _p_msb;
p_csb = _p_csb;
p_lsb = _p_lsb;
tempp = _tempp;
tempa = _tempa;
tempt = _tempt;
decimalPress = _decimalPress;
decimalAlt = _decimalAlt;
decimalTemp = _decimalTemp;
ptot = _ptot;
}
float MPL3115A2::getPressure()
{
I2c.write(m_i2c_address,0x26,0x00);
I2c.write(m_i2c_address,0x26,0x01);
delay(100);
I2c.read(m_i2c_address,0x1,3);
I2c.end();
while(I2c.available()){
p_msb=I2c.receive();
p_csb=I2c.receive();
p_lsb=I2c.receive();}
ptot=p_msb;
ptot=(ptot<<8)+p_csb;
ptot=(ptot<<2)+(p_lsb>>6);
tempp=(p_lsb<<2);
if(tempp==192){
decimalPress=75;
}else if(tempp==128){
decimalPress=25;
}else if(tempp==64){
decimalPress=5;
}else{tempp=0;}
Serial.print(ptot);
Serial.print(".");
Serial.println(decimalPress);
}
#endif
and my example.pde
#include <Arduino.h>
#include <MPL3115A2.h>
#include <I2C.h>
void setup(){
Serial.begin(9600);
I2c.begin();
}
//LOOP
void loop(){
Serial.print("Pressure is: ");
MPL3115A2.getPressure(); //Expressed in Pa
Serial.print("Altimetry is: ");
//MPL3115A2.getAltimetry(); //Expressed in m
Serial.print("Temperature is: ");
// MPL3115A2.getTemperature(); //Expressed in C
delay(2000);
}
Sorry for the long post! Thank you all for your help!
-
down vote cowards need to post their reasonspring– spring2012年11月22日 02:34:07 +00:00Commented Nov 22, 2012 at 2:34
2 Answers 2
getPressure()
has no return type! According to the cpp you posted, your header file should declare the method as float getPressure();
so change your header file to:
#ifndef MPL3115A2_h
#define MPL3115A2_h
#include "Arduino.h"
class MPL3115A2{
public:
float getPressure(); // <- only change here
private:
int m_i2c_address;
byte _p_msb, _p_csb, _plsb;
byte _tempp, _tempa, _tempt, _decimalPress, _decimalAlt, _decimalTemp;
unsigned long _ptot;
};
#endif
to correspond with the definition which is
float MPL3115A2::getPressure()
{
// your processing code...
}
Also, keep in mind that you should review your constructor
as it is not declared in your header. Also, doing something like
p_msb = _p_msb;
p_csb = _p_csb;
p_lsb = _p_lsb;
tempp = _tempp;
tempa = _tempa;
tempt = _tempt;
decimalPress = _decimalPress;
decimalAlt = _decimalAlt;
decimalTemp = _decimalTemp;
ptot = _ptot;
is really weird. What are the variables on the left? You are initializing them with your uninitialized members (those on the right, starting with _
). You should consider giving an initial value to these members (like 0
?)
Changing your constructor to something like:
MPL3115A2::MPL3115A2()
{
_p_msb = 0;
_p_csb = 0;
_p_lsb = 0;
_tempp = 0;
_tempa = 0;
_tempt = 0;
_decimalPress = 0;
_decimalAlt = 0;
_decimalTemp = 0;
_ptot = 0;
}
To make sure your member variables are initialized with a "known" value.
Also, most of these values seem temporary (you might not need them outside of a call to getPressure()
. In this case, consider moving all these bytes to the scope of the function instead of inside the sensor class to avoid using unnecessary memory - there is not that much available on Arduinos.
You will also need to add MPL3115A2();
in the public
part of your MPL3115A2
class, this is because you are defining a body for this constructor in the CPP file, therefore you need to declare it so that it's part of the object.
-
Or actually
float MPL3115A2::getPressure()
as can be seen inMPL3115A2.cpp
above :-)Anders R. Bystrup– Anders R. Bystrup2012年11月21日 13:14:05 +00:00Commented Nov 21, 2012 at 13:14 -
@emartel you were right of course - I deleted the answer - was distractedCaribou– Caribou2012年11月21日 13:48:13 +00:00Commented Nov 21, 2012 at 13:48
-
@8bit_Biker for example, your constructor assings
ptot
with_ptot
. In the code you provided,ptot
doesn't exist and_ptot
wasn't given an initial value yet!emartel– emartel2012年11月21日 19:06:07 +00:00Commented Nov 21, 2012 at 19:06 -
Sorry but I don't understand your advice. These are "temp" vars corresponding to the pressure/altitude/etc sensor's register (pressure most significant byte... and so on), so I thought to consider them as temporary variable. But you must consider I'm far for being a programmer!So, I really appreciate if you could show me how it is the most correct way to use it8bit_Biker– 8bit_Biker2012年11月21日 19:14:01 +00:00Commented Nov 21, 2012 at 19:14
-
1@8bit_Biker you probably just need to add
MPL3115A2();
right before yourfloat getPressure();
in your header file to declare that you will be providing a constructor :)emartel– emartel2012年11月21日 21:14:48 +00:00Commented Nov 21, 2012 at 21:14
I'm thinking that the getPressure()
method should be declared with the return type float
as you implement it in MPL3115A2.cpp
, so the 'h
file should have:
...
class MPL3115A2 {
public:
float getPressure();
// (and remember the constructor)
private:
...
}
Also, are you aware that the method doesn't actually return
anything? If that is the idea, you could make it a void getPressure()
and change the class definition accordingly.
Added: For future readers, the answer is available in the error message, albeit well hidden in cryptic stuff: "...ISO C++ forbids declaration of 'getPressure' with no type..."
Hope that helps.