'NewPing' does not name a type

Hello,

I'm trying to use the NewPing library ( Arduino Playground - NewPing Library ) but get this error when trying to Verify my code:

In file included from newping_distance_sensor.cpp:7:
newping_distance_sensor.h:26: error: ‘NewPing’ does not name a type

just when I try to declare a variable:

    class DistanceSensor : public DistanceSensorDriver
    {
        
    private:
	NewPing m_sensor;
    };

Countless people had this error because they didn't install the NewPing library but I have ! I can see it in Sketch > Import Library. AND I have the line

#include <NewPing.h>

at the top of my file (but the ".h" doesn't have the same color than "NewPing", is that normal?). I'm on Ubuntu 12.04

Any other idea?

Any other idea?

The usual one for people that post in the Programming section without posting all of there code. But, I'll be nice and keep that to myself. Hopefully, though, you have an idea what you need to do.

Did you restart the arduino IDE after you installed the library ?

It seems to me, there may be an issue that the NewPing class has no version of a constructor which takes zero arguments.

And your code might appear to require one. Hard to say for sure without seeing all of your code.

But, if that was so, you should get a different error message to the one that you got.

PaulS:
The usual one for people that post in the Programming section without posting all of there code.

That would be a lot of code!

But I found a minimal not-working example and was able to find the problem.

This code works:

in main.ini:

#include "Robot.h"
#include <NewPing.h>
Robot robot;

void setup()
{
  
}

void loop()
{
  
}

in Robot.h:

#include <NewPing.h>

class Robot 
{  
  public:
    Robot() : sensor(1,2,3) {}
  private:
    NewPing sensor;
};

but it doesn't work if I comment out "#include <NewPing.h>" in main.ini, and I don't understand why since #include <NewPing.h> is still at the top of Robot.h !

it doesn't work if I comment out "#include <NewPing.h>" in main.ini, and I don't understand why since #include <NewPing.h> is still at the top of Robot.h !

I think you will find that this is a quirk of how the Arduino IDE assembles the actual file to be compiled from the bits an pieces needed, and that it a requirement to #include all library files used by the program in the master program file whether or not they are #included in library files.

By the way, do you really have a file called main.ini containing code or was that a typo ?

UKHeliBob:
By the way, do you really have a file called main.ini containing code or was that a typo ?

Was a typo the file has another name.

OK good to know.

Edit: note this seems to be only with library files, my .h project files don't seem to have to be included in the master .ino file