using class inside a class

I would like to include an instance of "NewPing" in a class that I am making. In the getdistance method I need to call sonar.ping which I declare with NewPing sonar(PIN_TRIG, PIN_ECHO, MAX_DISTANCE); I'm not sure what to do from here though. I appreciate any tips!

/* robot.cpp */
#include "robot.h"

Robot::Robot(int pinTrig, int pinEcho)
{


}

float Robot::getDistance()
{
}

}
/* robot.h */
#include <NewPing.h>

class Robot
{
  public:
    Robot(int pinTrig, int pinEcho);
    
  private:
    float getdistance();
};

}

I'm not sure what to do from here though.

You're going to need to create an instance of the NewPing class in your Robot class. In the getDistance() method, you need to exercise the NewPing instance, and make it return the distance value.

Why is getDistance() private?

Also you're going to have to be sure to include NewPing.h in your sketch, or the IDE won't know to tell the compiler where to find it.

Sorry getDistance should not be private. But why would I want to init the NewPing object each time getDistance is called. Can't I init with the Robot constructor and then access it with getDistance?

No one is saying init your NewPing in getDistance, only use the instance of NewPing, that you created in your constructor, in getDistance.

Ok... I misunderstood. Thank you. Is this correct?

/* robot.h */
#include <NewPing.h>

class Robot
{
  public:
    Robot(int pinTrig, int pinEcho);
    float getdistance();
};
/* robot.cpp */
#include "robot.h"

#define MAX_DISTANCE 500

Robot::Robot(int pinTrig, int pinEcho)
{
   NewPing sonar(pinTrig, pinEcho, MAX_DISTANCE);
}

float Robot::getDistance()
{
   return this->sonar.ping();
}

I'd go for a pointer personally:

#include <NewPing.h>

class Robot
{
  private:
    NewPing *sonar;

  public:
    Robot(int pinTrig, int pinEcho);
    float getdistance();
};
#include "robot.h"

#define MAX_DISTANCE 500

Robot::Robot(int pinTrig, int pinEcho)
{
   sonar = new NewPing(pinTrig, pinEcho, MAX_DISTANCE);
}

float Robot::getDistance()
{
   return sonar->ping();
}
  1. What advantage is the pointer?
  2. I thought "new" wasn't allowed.

The advantage is that your way won't work. What / where is your "sonar" variable? How do you define the variable without creating the object at the same time? Answer: use a pointer and new.

There is nothing at all wrong with new. It's excessive use of new / destroy or malloc() and free() that are frowned upon. Single allocation of an object at constructor time is perfectly fine.

"The advantage is that your way won't work. " That made me laugh!! Thanks!

I'm confused because in a function "NewPing sonar(pinTrig, pinEcho, MAX_DISTANCE);" works. Why does that not work when in a class?

The sonar variable is local to the function that you are defining it in. You need a way to break that variable out of that function so you can reference it in other functions.

Do the fact that I declared it in class constructor doesn't make it visible to the rest of the class? I need to make it public or private correct?

I'm confused because in a function "NewPing sonar(pinTrig, pinEcho, MAX_DISTANCE);" works.

I suspect that you mean that in a sketch, that works. It works in a sketch, or in a function, because the instance is declared and initialized in a single statement.

In a class, the declaration goes in one file and the initialization goes in another. Therefore, it is not possible to declare and initialize in a single statement.

You could do the declaration and initialization in a single statement, as long as that single statement is executed every time you need the object/call a function that needs the object to do something. That hardly seems like the thing to do with your class.

Thanks all! I'll be working on it tonight!

Ok I used the class exactly as shown above and I get the following errors...

In file included from sketch_feb25a.ino:1:
D:\users\freak\My Documents\Arduino\progs\libraries\testclass/Robot.h:6: error: ISO C++ forbids declaration of 'NewPing' with no type
D:\users\freak\My Documents\Arduino\progs\libraries\testclass/Robot.h:6: error: expected ';' before '*' token

...any ideas?

Ok I found the problem. It was explained to me in post 3. Can anyone explain why that is needed though?

Here is a post from an FAQ I'm writing, it should explain it for you.

http://arduino.land/FAQ/content/1/3/en/what-does-the-ide-change-in-my-sketch.html

Ok I used the class exactly as shown above and I get the following errors...

With what sketch? Does the sketch also include NewPing.h? It needs to...

It does now. I acknowledged that was my problem in post 15.

pYro_65:
Here is a post from an FAQ I'm writing, it should explain it for you.

http://arduino.land/FAQ/content/1/3/en/what-does-the-ide-change-in-my-sketch.html

Not sure it makes sense to me but I understand that it does that. I have to assume that someone writing the IDE knows what they are doing, :slight_smile: