Go Down

Topic: custom library problem (Read 10521 times) previous topic - next topic

rickburgen

[size=12]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:[/size]
Code: [Select]
//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);
   }

 }


[size=12]Here is the class code:[/size]
Code: [Select]
#ifndef Sensor
#define Sensor

#include "WProgram.h"

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

};

#endif

Code: [Select]
#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()
{

}

AlphaBeta

Code: [Select]
     int sensorPin = 4;
     int sensorVal = 0;

is not allowed in a header unless:
Code: [Select]
     static const int sensorPin = 4;
     static const int sensorVal = 0;


rickburgen

#2
Dec 15, 2009, 08:35 pm Last Edit: Dec 15, 2009, 08:44 pm by manray Reason: 1
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

PaulS

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

PaulS

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.

rickburgen

let me start over and let me present my libraries.
[size=14]Here is the Sensor Library [/size]
Code: [Select]
#ifndef Sensor
#define Sensor

#include "WProgram.h"

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

};

#endif

Code: [Select]
#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()
{

}


[size=14]Here is the Wheel Library[/size]
Code: [Select]
#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

Code: [Select]
#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
Code: [Select]
#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

Code: [Select]
#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 '<anonymous class>' 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 '<anonymous class>' 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 '<anonymous class>' used as declaration

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

AlphaBeta

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

What is carOK?

rickburgen

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

PaulS

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

AlphaBeta

#9
Dec 16, 2009, 02:20 am Last Edit: Dec 16, 2009, 02:24 am by AlphaBeta Reason: 1
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]

rickburgen

@PaulS-how do I do that?

PaulS

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

AlphaBeta

#12
Dec 16, 2009, 02:32 am Last Edit: Dec 16, 2009, 02:32 am by AlphaBeta Reason: 1
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.
Code: [Select]

#define Example
class Example {
};

Preprocesses to:
Code: [Select]

class {
};


Which clearly is a compile error.
:)

rickburgen

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

rickburgen

#14
Dec 16, 2009, 04:28 am Last Edit: Dec 16, 2009, 11:41 pm by manray Reason: 1
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?

Go Up