Go Down

Topic: Object Pass by reference (Read 623 times) previous topic - next topic

johncc

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);
   }
   






AlphaBeta

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

johncc

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

johncc

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

Coding Badly

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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy