Pages: [1] 2 3 4   Go Down
Author Topic: custom library problem  (Read 10143 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 117
oh H4x!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

[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:
//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:
#ifndef Sensor
#define Sensor

#include "WProgram.h"

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

};

#endif
Code:
#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()
{

}
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 117
oh H4x!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: December 15, 2009, 02:44:38 pm by manray » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 117
oh H4x!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

#include "WProgram.h"

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

};

#endif
Code:
#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:
#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:
#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:
#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:
#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()':
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

What is carOK?
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 117
oh H4x!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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]
« Last Edit: December 15, 2009, 08:24:54 pm by AlphaBeta » Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 117
oh H4x!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@PaulS-how do I do that?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#define Example
class Example {
};
Preprocesses to:
Code:
class {
};

Which clearly is a compile error.
 smiley
« Last Edit: December 15, 2009, 08:32:42 pm by AlphaBeta » Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 117
oh H4x!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 117
oh H4x!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: December 16, 2009, 05:41:23 pm by manray » Logged

Pages: [1] 2 3 4   Go Up
Jump to: