[Solved] IOCTL NOT DECLARED ERROR

Omickey Asks: IOCTL NOT DECLARED ERROR
I am trying to take in bearing values from a QMC5883L with the help of a c++ script. I am trying to do this with the help of the i2c bus as the compass is an i2c device.

But I am facing multiple errors as shown below.

Are there any drivers that are missing or is there any header file that I have missed out ? I am not able to figure out the error. Any help would be really appreciated.

compassvalscpp.cpp

Code:
#include<iostream>
#include "mycompass.h"
#include<cmath>
#include<bits/stdc++.h>
#include<stdlib.h>
#include<stdio.h>
int main(){
compass * compass_sensor = new compass();
compass_sensor->start();
float x;
while(1){
    compass_sensor->update();
    x = compass_sensor->bearing;
    std::cout<<"bearing = "<<x;
    }
delete compass_sensor;
return 0;}

mycompass.h

Code:
#ifndef __COMPASS_H__  
#define __COMPASS_H__
#define QMC5883L_I2C_ADDR 0x0D
#define M_PI = 3.1415926535
#include<cmath>
#include<bits/stdc++.h>
#include<stdlib.h>
#include<stdio.h>
bool init;
class compass{
private:
    int i2c_fd;
    bool select_i2c_device(int fd, int addr, char * name);
    bool write_to_i2c(int fd, int reg, int val);
public:
    float bearing;
    int start();
    int update();
};
#endif
bool compass::select_i2c_device(int fd, int addr, char * name){
    if (ioctl(fd, I2C_SLAVE, addr) < 0) {       
        return false;}
    return true;}
bool compass::write_to_i2c(int fd, int reg, int val){
    char buf[2];
    buf[0]=reg;
    buf[1]=val;
    if (write(fd, buf, 2) != 2){
        return false;}
    return true;}
int compass::start(){
    init = true;
    if ((i2c_fd = open("/dev/i2c-1", O_RDWR)) < 0){
        return 0;}
    select_i2c_device(i2c_fd, QMC5883L_I2C_ADDR, "QMC5883L");
    write_to_i2c(i2c_fd, 0x0A, 0b10000000);
    write_to_i2c(i2c_fd, 0x0A, 0b00000001);
    write_to_i2c(i2c_fd, 0x0B, 0x01);
    write_to_i2c(i2c_fd, 0x09, 0b00000001);
    return 1;
}
int compass::update(){
    float angle = 0;
    unsigned char i2c_buf[16];
    i2c_buf[0] = 0x03;
    if ((write(i2c_fd, i2c_buf, 1)) != 1){
        return 0;}
    if (read(i2c_fd, i2c_buf, 6) != 6){
        return 0;} 
    else{
        short x = (i2c_buf[1] << 8) | i2c_buf[0];
        short y = (i2c_buf[3] << 8) | i2c_buf[2];
        angle = atan2(y, x) * 180 / M_PI;}
    bearing = angle;
    return 1;
}

error log

Code:
g++ -Wall -o "compassvalscpp" "compassvalscpp.cpp" (in directory: /home/pi/Desktop/picodes)
In file included from compassvalscpp.cpp:2:
mycompass.h: In member function u2018bool compass::select_i2c_device(int, int, char*)u2019:
mycompass.h:23:19: error: u2018I2C_SLAVEu2019 was not declared in this scope
     if (ioctl(fd, I2C_SLAVE, addr) < 0) {
                   ^~~~~~~~~
mycompass.h:23:9: error: u2018ioctlu2019 was not declared in this scope
     if (ioctl(fd, I2C_SLAVE, addr) < 0) {
         ^~~~~
mycompass.h: In member function u2018bool compass::write_to_i2c(int, int, int)u2019:
mycompass.h:30:9: error: u2018writeu2019 was not declared in this scope
     if (write(fd, buf, 2) != 2){
         ^~~~~
mycompass.h:30:9: note: suggested alternative: u2018fwriteu2019
     if (write(fd, buf, 2) != 2){
         ^~~~~
         fwrite
mycompass.h: In member function u2018int compass::start()u2019:
mycompass.h:35:36: error: u2018O_RDWRu2019 was not declared in this scope
  if ((i2c_fd = popen("/dev/i2c-1", O_RDWR)) < 0){
                                    ^~~~~~
mycompass.h:37:60: warning: ISO C++ forbids converting a string constant to u2018char*u2019 [-Wwrite-strings]
     select_i2c_device(i2c_fd, QMC5883L_I2C_ADDR, "QMC5883L");
                                                            ^
mycompass.h: In member function u2018int compass::update()u2019:
mycompass.h:48:10: error: u2018writeu2019 was not declared in this scope
     if ((write(i2c_fd, i2c_buf, 1)) != 1){
          ^~~~~
mycompass.h:48:10: note: suggested alternative: u2018fwriteu2019
     if ((write(i2c_fd, i2c_buf, 1)) != 1){
          ^~~~~
          fwrite
mycompass.h:50:9: error: u2018readu2019 was not declared in this scope
     if (read(i2c_fd, i2c_buf, 6) != 6){
         ^~~~
mycompass.h:50:9: note: suggested alternative: u2018freadu2019
     if (read(i2c_fd, i2c_buf, 6) != 6){
         ^~~~
         fread
Compilation failed.

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.