custom library problem

I have created a few custom libraries to make my code look cleaner.
But when I declare a variable to a class in the library I get this error---error: expected constructor, destructor, or type conversion before ';' token In function 'void loop()':.
Here is the code inside the Arduino Software:

//include libraries                                                                                                                                                                                                                                         
  #include <sensor.h>
  #include <wheel.h>
  #include <servoView.h>
  
  Sensor sight();
  Wheel wheels();
  ServoView proximity();
  
  void setup() {
    // set all the other pins you're using as outputs:
    
  }

  void loop() {
    
    if (sight.viewIsClear())
    {
      wheels.forward(120);
    }
    else
    {
      wheels.reverse(120);
    }

  }

Here is the class code:

#ifndef Sensor
#define Sensor

#include "WProgram.h"

class Sensor
{
  public:
    Sensor();
      ~Sensor();
      boolean viewIsClear();
  private:
      int sensorPin = 4;
      int sensorVal = 0;

};

#endif
#include "WProgram.h"
#include "sensor.h" 

Sensor::Sensor()
{

}

boolean Sensor::viewIsClear()
  {
   sensorVal = analogRead(sensorPin);
  
  if (sensorVal > 370)
  {
   carOK = false; 
   return false;
  }
   
  else
  return true; 
  }

Sensor::~Sensor()
{

}
      int sensorPin = 4;
      int sensorVal = 0;

is not allowed in a header unless:

      static const int sensorPin = 4;
      static const int sensorVal = 0;

I still get the error on the sight variable for class Sensor after changing to static const. I also noticed that I get an error when I try to include the Servo.h file into one of the libraries. This is the error for that /Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/Direction/servoView.h:10:19: error: Servo.h: No such file or directory

Why not separate the declaration from the initialization? Initialize the members in the constructor.

Looking at this again, the sensorVal variable is written to, so it can't be declared const. Declaring it static means you can use it like this:

int probablyNotUseful = Sensor.sensorVal;

I doubt that this is what you intend for the users of your class to do, so you probably shouldn't declare the values as static.

let me start over and let me present my libraries.
Here is the Sensor Library

#ifndef Sensor
#define Sensor

#include "WProgram.h"

class Sensor
{
  public:
    Sensor();
      ~Sensor();
      boolean viewIsClear();
  private:
      int sensorPin;
      int sensorVal;

};

#endif
#include "WProgram.h"
#include "sensor.h" 

Sensor::Sensor()
{
      sensorPin = 4;
      sensorVal = 0;
}

boolean Sensor::viewIsClear()
  {
   sensorVal = analogRead(sensorPin);
  
  if (sensorVal > 370)
  {
   carOK = false; 
   return false;
  }
   
  else
  return true; 
  }

Sensor::~Sensor()
{

}

Here is the Wheel Library

#ifndef Wheel
#define Wheel

#include "WProgram.h"

class Wheel
{
  public:
    Wheel();
      ~Wheel();
    void controlRightFront(boolean one, boolean two, int x);
      void controlRightRear(boolean one, boolean two, int x);
      void controlLeftFront(boolean one, boolean two, int x);
      void controlLeftRear(boolean one, boolean two, int x);
      void forward(int x);
      void slowDown(boolean one, boolean two, int x);
      void halt();
      boolean forwardDrive;
      boolean reverseDrive;

  private:
    int rightfrontmotorOne;
      int rightfrontmotorTwo;
      int rightrearmotorOne;
      int rightrearmotorTwo;
      int leftfrontmotorOne;
      int leftfrontmotorTwo;
      int leftrearmotorOne;
      int leftrearmotorTwo;
      int enablePinRF;
      int enablePinRR;
      int enablePinLF;
      int enablePinLR;

};

#endif
#include "WProgram.h"
#include "Wheel.h"

Wheel::Wheel()
{
      rightfrontmotorOne = 0;
      rightfrontmotorTwo = 1;
      rightrearmotorOne = 8;
      rightrearmotorTwo = 12;
      leftfrontmotorOne = 2;
      leftfrontmotorTwo = 3;
      leftrearmotorOne = 4;
      leftrearmotorTwo = 7;
      enablePinRF = 2;
      enablePinRR = 4;
      enablePinLF = 5;
      enablePinLR = 3;
      
      pinMode(rightfrontmotorOne, OUTPUT);
    pinMode(rightfrontmotorTwo, OUTPUT);
    pinMode(rightrearmotorOne, OUTPUT);
    pinMode(rightrearmotorTwo, OUTPUT); 
    pinMode(leftfrontmotorOne, OUTPUT);
    pinMode(leftfrontmotorTwo, OUTPUT);
    pinMode(leftrearmotorOne, OUTPUT);
    pinMode(leftrearmotorTwo, OUTPUT);
    pinMode(enablePinRF, OUTPUT);
    pinMode(enablePinRR, OUTPUT);
    pinMode(enablePinLF, OUTPUT);
    pinMode(enablePinLR, OUTPUT);
}

Wheel::~Wheel()
{
      
      
}

void Wheel::controlRightFront(boolean one, boolean two, int x)
{
  analogWrite(enablePinRF, x);
  digitalWrite(rightfrontmotorOne, one);
  digitalWrite(rightfrontmotorTwo, two);
}
  
void Wheel::controlRightRear(boolean one, boolean two, int x)
{
  analogWrite(enablePinRR, x);
  digitalWrite(rightrearmotorOne, one);
  digitalWrite(rightrearmotorTwo, two);
}
  
void Wheel::controlLeftFront(boolean one, boolean two, int x)
{
  analogWrite(enablePinLF, x);
  digitalWrite(leftfrontmotorOne, one);
  digitalWrite(leftfrontmotorTwo, two);
}
  
void Wheel::controlLeftRear(boolean one, boolean two, int x)
{
  analogWrite(enablePinLR, x);
  digitalWrite(leftrearmotorOne, one);
  digitalWrite(leftrearmotorTwo, two); 
}
  
void Wheel::reverse(int x)
{
  if (forwardDrive == true)
  {
  halt();
  delay(500);
  }
  reverseDrive = true;
  controlRightFront(LOW,HIGH,x);
  controlRightRear(LOW,HIGH,x);
  controlLeftFront(LOW,HIGH,x);
  controlLeftRear(LOW,HIGH,x); 
  forwardDrive = false;
}
  
void Wheel::forward(int x)
{
  if (reverseDrive == true)
  {
    halt();
    delay(500);
  }
  forwardDrive = true;
  controlRightFront(HIGH,LOW,x);
  controlRightRear(HIGH,LOW,x);
  controlLeftFront(HIGH,LOW,x);
  controlLeftRear(HIGH,LOW,x);
  reverseDrive = false;
}
  
void Wheel::halt()
{
  controlRightFront(LOW,LOW,0);
  controlRightRear(LOW,LOW,0);
  controlLeftFront(LOW,LOW,0);
  controlLeftRear(LOW,LOW,0);
}
  
void Wheel::slowDown(boolean one, boolean two, int x)
{
  controlRightFront(one,two,x);
  controlRightRear(one,two,x);
  controlLeftFront(one,two,x);
  controlLeftRear(one,two,x);
}

Here is the ServoView Class

#ifndef ServoView
#define ServoView

#include "WProgram.h"
#include <Servo.h>

class ServoView
{
  public:
    ServoView();
      ~ServoView();
      void StartRotateServo(int x);
      void StopRotateServo();
  private:
      Servo mainServo;
      int servoPin;
      int servoVal;

};

#endif
#include "WProgram.h"
#include "servoView.h" 
#include <Servo.h>

ServoView::ServoView()
{
      servoPin = 13;
      servoVal = 0;
      mainServo.attach(servoPin);

}

void ServoView::StartRotateServo(int x)
{
  mainServo.write(x);
}
  
void ServoView::StopRotateServo()
{
  mainServo.write(); 
}

ServoView::~ServoView()
{
      mainServo.detach(servoPin);
}

And Here are the Errors I am getting
/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/Direction/servoView.h:10:19: error: Servo.h: No such file or directory

/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/Sensor/sensor.h:14: error: expected unqualified-id before ')' token

/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/Sensor/sensor.h:15: error: expected class-name before '(' token

/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/Sensor/sensor.h:12: error: an anonymous struct cannot have function members

/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/Sensor/sensor.h:21: error: abstract declarator '' used as declaration

/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/Wheel/wheel.h:14: error: expected unqualified-id before ')' token

/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/Wheel/wheel.h:15: error: expected class-name before '(' token

/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/Wheel/wheel.h:12: error: an anonymous struct cannot have function members

/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/Wheel/wheel.h:40: error: abstract declarator '' used as declaration

/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/Direction/servoView.h:15: error: expected unqualified-id before ')' token

/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/Direction/servoView.h:16: error: expected class-name before '(' token

/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/Direction/servoView.h:20: error: 'Servo' does not name a type

/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/Direction/servoView.h:13: error: an anonymous struct cannot have function members

/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/Direction/servoView.h:24: error: abstract declarator '' used as declaration

error: expected constructor, destructor, or type conversion before ';' token In function 'void loop()':

It seems you have several cases of undeclared identifiers.
Such as:
carOK = false; in you sensor.cpp

What is carOK?

its a loose variable that I am going to be using soon. But that is not causing all these problems?

What happens if you change the #include <Servo.h> line to an explicit path to the Servo.h file?

All undeclared variables will cause compile errors. This is because the compiler does not know what these symbols are, and refuses to guess.
[edit]You also need to chage all:

#ifndef ClassName
#define ClassName

pairs to:

#ifndef ClassName_h
#define ClassName_h

You can refer to my How to write a library for the Arduino article on the playground.[/edit]

@PaulS-how do I do that?

#include <../ServoPath/Servo.h>
or
#include <C:\Wherever\the\servo\library\is\Servo.h>

I think the problem is not that the compiler does not find Servo, I think something corrupts the compilation in such a way that the compiler thinks it did not find Servo.

As you have coded now, you basically remove the name of your classes.

#define Example
class Example {
};

Preprocesses to:

class {
};

Which clearly is a compile error.
:slight_smile:

I don't know what I did, but the only error I am getting now is including the servo library.

OK, I fixed everything related to what I posted prior. I am now getting

In function 'void loop()':
error: request for member 'viewIsClear' in 'sight', which is of non-class type 'Sensor ()()

does this mean that Sensor is not a class?

Hi,
the problem with
#include <Servo.h>
is that you cannot include an existing Arduino library to a new library that you write.

see here
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1255235742
I guess this needs to be documented in the Library tutorial.

Eberhard

the problem with
#include <Servo.h>
is that you cannot include an existing Arduino library to a new library that you write.

It is (sort of) possible...
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1260640627/5#5

The include needs either a relative or absolute path (my preference is for the relative one)...

#include "..\Servo\Servo.h"

And the header has to be included in the Sketch in the usual way.

i found out, that if you put a copy of the libraries you want to include in the directory of your written library it works. Not nice style, but works.

I am now getting
In function 'void loop()':
error: request for member 'viewIsClear' in 'sight', which is of non-class type 'Sensor ()()

I can't see the problem. Hopefully, someone with younger eyes will spot it...

It is (sort of) possible...
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1260640627/5#5

Not here (Arduino-0017) ....

If I put the include statement into one of my custom libs the compiler will not complain any more, because it can read the file following the relative path.
But looking at the compiler output there is no sign that the servo-library is compiled and linked into the code to be uploaded!

Did you actually try to use any code from the included Servo-lib in your custom library.

Here is (part of) the compiler output when compiling a sketch that uses the DogLcd-lib which includes the Servo.h file and tries to create an instance of class Servo.

avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -I/home/wayoda/arduino/arduino-0017/hardware/cores/arduino -I/home/wayoda/arduino/arduino-0017/hardware/libraries/DogLcd -I/home/wayoda/arduino/arduino-0017/hardware/libraries/DogLcd/utility /home/wayoda/arduino/arduino-0017/hardware/libraries/DogLcd/DogLcd.cpp -o/tmp/build1691778465959691824.tmp/DogLcd/DogLcd.cpp.o 
avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -I/home/wayoda/arduino/arduino-0017/hardware/cores/arduino -I/home/wayoda/arduino/arduino-0017/hardware/libraries/DogLcd /tmp/build1691778465959691824.tmp/HelloWorld.cpp -o/tmp/build1691778465959691824.tmp/HelloWorld.cpp.o 
avr-gcc -Os -Wl,--gc-sections -mmcu=atmega328p -o /tmp/build1691778465959691824.tmp/HelloWorld.cpp.elf /tmp/build1691778465959691824.tmp/DogLcd/DogLcd.cpp.o /tmp/build1691778465959691824.tmp/HelloWorld.cpp.o /tmp/build1691778465959691824.tmp/core.a -L/tmp/build1691778465959691824.tmp -lm 

/tmp/build1691778465959691824.tmp/DogLcd/DogLcd.cpp.o: In function `DogLcd::DogLcd(int, int, int, int, int, int)':

/home/wayoda/arduino/arduino-0017/hardware/libraries/DogLcd/DogLcd.cpp:8: undefined reference to `Servo::Servo()'

There is no trace of the Servo-lib being compiled and linked.
Eberhard