Pages: [1]   Go Down
Author Topic: Object Pass by reference  (Read 517 times)
0 Members and 1 Guest are viewing this topic.
Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Fairly new to Arduino, and its been a few years since c++, but....

I get the error messages below.  It doesn't say what line but it seems to be complaining about my transition() function:
Quote
Traffic:-1: error: variable or field 'transition' declared void
Traffic:-1: error: 'TLight' was not declared in this scope
Traffic:-1: error: 'toRed' was not declared in this scope
Traffic:-1: error: 'TLight' was not declared in this scope
Traffic:-1: error: 'toGreen' was not declared in this scope
Traffic:-1: error: expected primary-expression before 'int'

Does the arduino version of the compiler support pass by reference, or,  what am I missing...

Thanks!!  Having a great time with this with my nieces!

John
Quote
class TLight {
          private:
      int rP,yP,gP;
      int doubleRedP;
          public:
      TLight(int red, int yellow, int green, int extraRed)
      {
         rP = red; yP = yellow; gP = green; doubleRedP = extraRed;
         pinMode(rP, OUTPUT);
         pinMode(yP, OUTPUT);
         pinMode(gP, OUTPUT);
      }

      TLight(int red, int yellow, int green)
      {
         rP = red; yP = yellow; gP = green; doubleRedP = -1;
         pinMode(rP, OUTPUT);
         pinMode(yP, OUTPUT);
         pinMode(gP, OUTPUT);
      }

      void red()   { set(HIGH, LOW, LOW); }
      void yellow(){ set(LOW, HIGH, LOW); }
      void green() { set(LOW, LOW, HIGH); }
      
            private:
      void set(int rV, int yV, int gV){
         digitalWrite(rP, rV);
         digitalWrite(yP, yV);
         digitalWrite(gP, gV);
         if (doubleRedP >= 0)
            digitalWrite(doubleRedP, rV);
      }
   };

   TLight t1( 9, 11, 13);      
        //TLight t2( 5,6,7);

   void transition( TLight & toRed, TLight& toGreen, int yellowDelay)
   {
      toRed.yellow(); 
      delay(yellowDelay);
      toRed.red();
      toGreen.green();
   }

   // The setup() method runs once, when the sketch starts
   void setup()   {               
      t1.green();
   //   t2.red();
   }


        void loop()                     
   {
            //transition(t1, t2, 1000); delay(4000);
      //transition(t2, t1, 1000); delay(4000);
   }
   




Logged

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

It's a problem with the IDE, try to author your class and your transition function in a different tab smiley
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure what the IDE does and doesn't do for me w.r.t. header files, etc.

Now I have Traffic.pde:
Quote
TLight t1( 9, 11, 13);      
//TLight t2( 5,6,7);

void setup()   {               
  t1.green();
  // t2.red();
}
void loop()                     
{
  //transition(t1, t2, 1000); delay(4000);
  //transition(t2, t1, 1000); delay(4000);
}
and TLight.pde in a separate "tab":
Quote
class TLight {
private:
  int rP,yP,gP;
  int doubleRedP;
public:
  TLight(int red, int yellow, int green, int extraRed)
  {
    rP = red; 
    yP = yellow; 
    gP = green; 
    doubleRedP = extraRed;
    pinMode(rP, OUTPUT);
    pinMode(yP, OUTPUT);
    pinMode(gP, OUTPUT);
  }

  TLight(int red, int yellow, int green)
  {
    rP = red; 
    yP = yellow; 
    gP = green; 
    doubleRedP = -1;
    pinMode(rP, OUTPUT);
    pinMode(yP, OUTPUT);
    pinMode(gP, OUTPUT);
  }

  void red()   {
    set(HIGH, LOW, LOW);
  }
  void yellow(){
    set(LOW, HIGH, LOW);
  }
  void green() {
    set(LOW, LOW, HIGH);
  }

private:
  void set(int rV, int yV, int gV){
    digitalWrite(rP, rV);
    digitalWrite(yP, yV);
    digitalWrite(gP, gV);
    if (doubleRedP >= 0)
      digitalWrite(doubleRedP, rV);
  }
};

void transition( TLight & toRed, TLight& toGreen, int yellowDelay)
{
  toRed.yellow(); 
  delay(yellowDelay);
  toRed.red();
  toGreen.green();
}

And I still get:



Traffic:-1: error: variable or field 'transition' declared void
Traffic:-1: error: 'TLight' was not declared in this scope
Traffic:-1: error: 'toRed' was not declared in this scope
Traffic:-1: error: 'TLight' was not declared in this scope
Traffic:-1: error: 'toGreen' was not declared in this scope
Traffic:-1: error: expected primary-expression before 'int'
Traffic:0: error: 'TLight' does not name a type
Traffic.cpp: In function 'void setup()':
Traffic:4: error: 't1' was not declared in this scope
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, it took me an hour to figure out that I need to include "WProgram.h" in my other file.  So for the sake of posterity, here is the configuration:
Traffic ("Main Tab"):
Quote
#include "TLight.h"

TLight t1( 9, 11, 13);      
TLight t2( 5,6,7);

void setup()   
{                
  t1.green();
  t2.red();
}
void loop()                     
{
  transition(t1, t2, 1000); delay(4000);
  transition(t2, t1, 1000); delay(4000);
}

void transition( TLight & toRed, TLight& toGreen, int yellowDelay)
{
  toRed.yellow(); 
  delay(yellowDelay);
  toRed.red();
  toGreen.green();
}

TLight.h:
Quote
#include "WProgram.h"

class TLight {
    .... etc, as before
};

Cheers,
John
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 209
Posts: 13027
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Calling core functions in an object constructor can cause serious problems...

Quote
 TLight(int red, int yellow, int green)
  {
...
    pinMode(rP, OUTPUT);
    pinMode(yP, OUTPUT);
    pinMode(gP, OUTPUT);
  }

Object constructors are called before the hardware has been initialized.  Add a begin function that initializes the hardware then call begin in setup.  In other words, use Serial as an example of how to do things correctly.
Logged

Pages: [1]   Go Up
Jump to: