Search code examples
c++classcompiler-errorspopulate

Including a class object inside another class c++


I am having trouble calling a method on an object created inside another classes private scope. The error is down the bottom as well. It seems like it is not recognising the Motor variables inside my car class.

Do I need to create them as pointers? Or is there something else I am missing here?

This is to be used in an Arduino project.

Motor.h:

    /*
 * Library for turning a motor
 * turns forward reverse and stops
 */
#ifndef Motor_h
#define Motor_h

class Motor {
  private:
    int _fPin;
    int _bPin;
  public:
    Motor();
    void init(int fPin, int bPin);

    void forward();

    void back();

    void brake();
};

#endif

Car.h

#include "Motor.h"

class Car
{
  private:
    Motor rMotor;
    Motor lMotor;
    float degPerMs = 4.44;

  public:
  
    Car(int forR, int revR, int forL, int revL);

    void forward();

    void reverse();

    //input from -180 to 180
    void turn(int angle);

    void brake();
};

Car.cpp;

#include "Car.h"
#include "Motor.h"
#include "Arduino.h"

Car::Car(int forR, int revR, int forL, int revL)
{
    rMotor.init(int forR, int revR);
    lMotor.init(int forL, int revL);
};

Car::forward()
{
    rMotor.forward();
    lMotor.forward();
}

Car::reverse()
{
    rMotor.back();
    lMotor.back();
};

Car::turn(int angle)
{

    float onTime = abs(angle) * degPerMs;

    if (angle < 0)
    {
        rMotor.forward();
        lMotor.back();
        delay(onTime);
        rMotor.brake();
        lMotor.brake();
    }
    else
    {
        lMotor.forward();
        rMotor.back();
        delay(onTime);
        rMotor.brake();
        lMotor.brake();
    }
};

Car::brake()
{
    lMotor.brake();
    rMotor.brake();
};

errors

D:\4. Study\Deakin\2nd Year\Trimester 2\SIT217 - Engineering 1 Robotics Project\2. Project\main\Car.cpp: In constructor 'Car::Car(int, int, int, int)':
Car.cpp:7:17: error: expected primary-expression before 'int'
     rMotor.init(int forR, int revR);
                 ^~~
Car.cpp:7:27: error: expected primary-expression before 'int'
     rMotor.init(int forR, int revR);
                           ^~~
Car.cpp:8:17: error: expected primary-expression before 'int'
     lMotor.init(int forL, int revL);
                 ^~~
Car.cpp:8:27: error: expected primary-expression before 'int'
     lMotor.init(int forL, int revL);
                           ^~~
exit status 1
expected primary-expression before 'int'

Solution

  • In methods definition like in

    Car::Car(int forR, int revR, int 
    forL, int revL)
    {
        rMotor.init(int forR, int revR);
        lMotor.init(int forL, int revL);
    };
    

    you are calling init, not defining it. So it is wrong specifying the type of the parameters (int) and it results in a set of compilation errors.

    Just call the methods in this way, just passing to motor::init the variables coming from Cars constructor:

    Car::Car(int forR, int revR, int 
    forL, int revL)
    {
        rMotor.init(forR, revR);
        lMotor.init(forL, revL);
    };