Creating a library with different libary

I am doing an IOT project, so I am using multiple different libraries for my different hardware. I want to make my main look clean and simple so I want to put all the functions for the sensor into one library and call it with one function in the begin and loop. However, I have encountered an error, which is "expected unqualified-id before '.' token" at several lines of the code. I have added comments where it says the error is. And also, it says that I do not I have a class type for mq2 in line 8 of the cpp file

"'((firealarm*)this)->firealarm::mq2' does not have class type mq2.begin();"

main file

#include "firealarm.h"

void setup() {
  firealarm.begin();
}

void loop() {
  firealarm.gas_values(); //error here
}

header file

#include <MQ2.h>

#ifndef _firealarm
#define _firealarm

#if ARDUINO >= 100
#include "Arduino.h"
#endif

class firealarm {
  public:
    firealarm();
    void begin(int baudrate = 9600);
    void gas_values();
    
    int lpg, co, smoke;
  private:
    MQ2 mq2(int mqpin = 14);

};
#endif

cpp file

#include "firealarm.h"
firealarm::firealarm() {
}
void firealarm::begin(int baudrate) {
  
  Serial.begin(baudrate);
  Serial.println("Firealarm libary created successfully");
  mq2.begin();
}
void firealarm::gas_values() {
  float* values = mq2.read(true); //error here
  lpg = mq2.readLPG(); //error here
  co = mq2.readCO(); //error here
  smoke = mq2.readSmoke(); //error here

}

code.txt (661 Bytes)

You don't appear to be creating an instance of the firealarm object in the main sketch

Another observation.

baudrate is declared as an int but what if someone wants to use a baud rate of say 115200 that won't fit in an int ?

I tried doing an instance in my main, however the same error still persists.

#include "firealarm.h"

firealarm firealarm
void setup() {
firealarm.begin();
}

void loop() {
firealarm.gas_values();
}

Bad idea to have an instance with the same name as the class. You also left out the ';'.

cheers mate, that solve the "expected unqualified-id" error. However, the does not have classes error is still there whenever I called it in my cpp file.

Post your updated code. Also, for posting and debug purposes, just put everything in one file:

// --------- .h file contents -------

//--------- .cpp file contents -----

//---------.ino file contents -------

Leave out the #include guards and the #include "firealarm.h" directives

Much easier for others to provide help that way.

You can break it into separate files after debugging.

I have updated the code. Right now the current problem is it saying I do not have the class type whenever I try calling a function from a different library. the error says
"firealarm.cpp:10:3: error: '((firealarm*)this)->firealarm::mq2' does not have class type"

//-----------main file-------------

#include "firealarm.h"
firealarm Falarm;

void setup() {
  Falarm.begin();
}

void loop() {
  Falarm.gas_values();
}

//--------------h file----------

#include <MQ2.h>

#ifndef _firealarm_
#define _firealarm_

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

class firealarm {
  public:
    firealarm();
    void begin(int baudrate);
    void gas_values();

    MQ2 mq2(int pin);

    int lpg, co, smoke;
  private:

};
#endif

//----------cpp file----------

#include "firealarm.h"
#include "Arduino.h"
firealarm::firealarm() {
  MQ2 mq2(14);
}
void firealarm::begin(int baudrate) {

  Serial.begin(baudrate);
  Serial.println("Firealarm libary created successfully");
  mq2.begin();
}
void firealarm::gas_values() {
  float* values = mq2.read(true);
  lpg = mq2.readLPG();
  co = mq2.readCO();
  smoke = mq2.readSmoke();

}

code.txt (661 Bytes)

That’s not how you instantiate a member variable that’s an instance of a class and whose constructor takes a value. I corrected that for you. See below. Also, see what I meant when I requested you to put everything in one file? That way people don’t have to create 3 separate files in order to compile your code. You stand the best chance of getting help if you make it easy for people to help you. You can separate them later.

Also, your ‘begin()’ method claims to take an integer parameter, but then you try to call it from ‘setup()’ without a argument. You need to fix that before it will compile. By the way, why is it an integer? Do you ever expect the baud to be negative? What happens if you want a baud rate greater than 32767?

#include <MQ2.h>
#include "Arduino.h"

class firealarm {
  public:
    firealarm();
    void begin(int baudrate);
    void gas_values();

    MQ2 mq2;

    int lpg, co, smoke;
  private:
};

firealarm::firealarm() : mq2(14){
}
void firealarm::begin(int baudrate) {
  Serial.begin(baudrate);
  Serial.println("Firealarm libary created successfully");
  //mq2.begin();
}

void firealarm::gas_values() {
  float* values = mq2.read(true);
  lpg = mq2.readLPG();
  co = mq2.readCO();
  smoke = mq2.readSmoke();
}

firealarm Falarm;

void setup() {
  Falarm.begin();
}

void loop() {
  Falarm.gas_values();
}

What happens if you want a baud rate greater than 32767?

I got no answer to a similar question in an earlier post

Oh I get it now. About the all the code in one file, I thought you meant to put it all in one text file and insert in the comments. My mistake, sorry about that.

about the int baudrate, I was looking at a tutorial on youtube and he call it that way just to proof he can call and set the variable in the h file.