Getting an error that a call to constructor is not correct

Hello
I am trying to use libraries to encapsulate implementation details of certain classes so it is easier to decompose objects and separation of concerns.
In this situation, I am hiding implementation details of newPing objects that's in class of UltraSonicSensor.

I get the following message error when I try to build this:

/home/pavel/ZumoArduino/libraries/UltraSonicSensor/UltraSonicSensor.cpp: In constructor 'UltraSonicSensor::UltraSonicSensor()':
/home/pavel/ZumoArduino/libraries/UltraSonicSensor/UltraSonicSensor.cpp:11:36: error: no matching function for call to 'NewPing::NewPing()'
 UltraSonicSensor::UltraSonicSensor() {
                                    ^
/home/pavel/ZumoArduino/libraries/UltraSonicSensor/UltraSonicSensor.cpp:11:36: note: candidates are:
In file included from /home/pavel/ZumoArduino/libraries/UltraSonicSensor/UltraSonicSensor.h:5:0,
                 from /home/pavel/ZumoArduino/libraries/UltraSonicSensor/UltraSonicSensor.cpp:3:
/home/pavel/ZumoArduino/libraries/NewPing/NewPing.h:206:3: note: NewPing::NewPing(uint8_t, uint8_t, unsigned int)
   NewPing(uint8_t trigger_pin, uint8_t echo_pin, unsigned int max_cm_distance = MAX_SENSOR_DISTANCE);

   ^
/home/pavel/ZumoArduino/libraries/NewPing/NewPing.h:206:3: note:   candidate expects 3 arguments, 0 provided
/home/pavel/ZumoArduino/libraries/NewPing/NewPing.h:204:7: note: constexpr NewPing::NewPing(const NewPing&)
 class NewPing {

       ^
/home/pavel/ZumoArduino/libraries/NewPing/NewPing.h:204:7: note:   candidate expects 1 argument, 0 provided
/home/pavel/ZumoArduino/libraries/NewPing/NewPing.h:204:7: note: constexpr NewPing::NewPing(NewPing&&)
/home/pavel/ZumoArduino/libraries/NewPing/NewPing.h:204:7: note:   candidate expects 1 argument, 0 provided
/home/pavel/ZumoArduino/libraries/UltraSonicSensor/UltraSonicSensor.cpp:12:41: error: no match for call to '(NewPing) (int, int, int)'
     UltraSonicSensor::_sonar(12, 13, 400);
                                         ^
exit status 1
Error compiling for board Arduino/Genuino Uno.

This is the following files that I just implemented:

UltraSonicSensor.h

#ifndef UltraSonicSensor_h
#define UltraSonicSensor_h

#include "Arduino.h"
#include "NewPing.h"

class UltraSonicSensor
{
public:
    UltraSonicSensor();
    /*int getCurrentReading();
    bool hasDetectedContact();*/
private:
    NewPing _sonar;
    int _TRIGGER_PIN;
    int _ECHO_PIN;
    int _MAX_DISTANCE;
};

#endif

UltraSonicSensor.cpp

#include "Arduino.h"
#include "NewPing.h"
#include "UltraSonicSensor.h"


/*#define TRIGGER_PIN 12
#define ECHO_PIN
#define MAX_DISTANCE*/


UltraSonicSensor::UltraSonicSensor() {
    UltraSonicSensor::_sonar(12, 13, 400);
}

and the running file of zujmo-rescuer.ino

#include <UltraSonicSensor.h>

void setup() {
  // put your setup code here, to run once:
  String var = "Hello";
}

void loop() {
  // put your main code here, to run repeatedly:

}

and here's the link to the reference of the NewPing: Arduino Playground - NewPing Library

and the directory structure is:
zumo-rescuer
-- zumo-rescuer
libraries
--UltraSonicSensor
----UltraSonicSensor.cpp
----UltraSonicSensor.h
--NewPing
----NewPing

Thank you very much for reading this!

NewPing.cpp (16.6 KB)

NewPing.h (13.8 KB)

I would use an initializer list and definitely different names.

#include <NewPing.h>

class UltraSonicSensor {
  public:
    UltraSonicSensor(byte tp, byte ep, int maxDist) :
      _TRIGGER_PIN(tp), _ECHO_PIN(ep), _MAX_DISTANCE(maxDist), _sonar(tp, ep, maxDist) {}
  private:
    byte _TRIGGER_PIN;
    byte _ECHO_PIN;
    int _MAX_DISTANCE;
    NewPing _sonar;
};

UltraSonicSensor us(12, 13, 400);

void setup() {}
void loop() {}

Since you have NewPing _sonar in your UltraSonicSensor class, it is created before your UltraSonicSensor constructor is called.

The compiler is expecting to find a constructor for NewPing like:

NewPing::NewPing()

if you rename NewPing::NewPing() to NewPing::initialize()

then your UltraSonicSensor constructor will be:

UltraSonicSensor::UltraSonicSensor() {
    _sonar.initialize(12, 13, 400);
}

You should also add a NewPing::NewPing() constructor, even if it doesn't now do much.

Yours,
TonyWilk

TonyWilk:
You should also add a NewPing::NewPing() constructor, even if it doesn't now do much.

I don't think changing libraries is a good idea,
especially if the need to do so is poor syntax knowledge.

Whandall:
I don't think changing libraries is a good idea,
especially if the need to do so is poor syntax knowledge.

Yes, I reckon you're right there.

pavelexpertov - ignore my suggestion and go with Whandall's

The initializers tell the compiler how to make all the things in UltraSonicSensor, and then your constructor is called in case you need to do anything else.

Yours,
TonyWilk

Thank you very much guys!!
Really appreciate it! It's a shame about my rusty knowledge of c++ since I used to program in it.

I am curious, is it possible to define constants within the class so I don't have to pass it down to constructor for encapsulation purposes?
So I am thinking to use #define so that I can place the constant variables to the initialiser's list of initial constructors calls that in turn the constant variables can be replaced with specific values.

Thanks though!

pavelexpertov:
Thank you very much guys!!
Really appreciate it! It's a shame about my rusty knowledge of c++ since I used to program in it.

I am curious, is it possible to define constants within the class so I don't have to pass it down to constructor for encapsulation purposes?
So I am thinking to use #define so that I can place the constant variables to the initialiser's list of initial constructors calls that in turn the constant variables can be replaced with specific values.

Thanks though!

look at defining a static variable in the class and assigning it in the implementation file.