0
votes

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!

2
down vote cowards need to post their reasonspring

2 Answers

2
votes

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.

1
votes

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.