Go Down

Topic: How to give an Object as an Argument to a Function? (...not to a class method) (Read 919 times) previous topic - next topic

GreyGnome

Hi,
I am trying to give an object as an argument to a function, but I am getting an error:

Code: [Select]

class myclass
{
  public:
 
  void yo() {
    Serial.println("Hello");
  }
};

void reportit(pbs& i) {
  //i.yo(); // If this didn't give an error, I would uncomment this method call.
  Serial.print("test");
};


void setup()
{
  Serial.begin(115200); Serial.println("---------------------------------------");
};

void loop() {
};


But it gives:

tmp:-1: error: variable or field 'reportit' declared void
tmp:-1: error: 'pbs' was not declared in this scope
tmp:-1: error: 'i' was not declared in this scope
tmp:10: error: variable or field 'reportit' declared void
tmp:10: error: 'pbs' was not declared in this scope
tmp:10: error: 'i' was not declared in this scope

Can anyone help?  Thanks.

pYro_65

Should move this too the programming questions forum.

pbs is not an object... easier to show code fixes.

Code: [Select]
class myclass
{
 public:
 
 void yo() {
   Serial.println("Hello");
 }
};

void reportit(myclass &i) { //cant make up class names pbs??
 i.yo(); // If this didn't give an error, I would uncomment this method call.
 Serial.print("test");
};


void setup()
{
 Serial.begin(115200); Serial.println("---------------------------------------");
};

void loop() {
 
 myclass m_Class; //Class instance
 reportit( m_Class ); //Pass to function
};


Arduino IDE get confused with classes in the sketch so create this file below and put it in your sketch folder ( Ctrl+K from IDE )

myclass.h
Code: [Select]


#include <Arduino.h>

class myclass
{
 public:
 
 void yo() {
   Serial.println("Hello");
 }
};


and in your sketch file
Code: [Select]

#include "myclass.h"


void reportit(myclass &i) { //cant make up class names pbs??
 i.yo(); // If this didn't give an error, I would uncomment this method call.
 Serial.print("test");
}


void setup()
{
 Serial.begin(115200); Serial.println("---------------------------------------");
}

void loop() {
 
 myclass m_Class; //Class instance
 reportit( m_Class ); //Pass to function
}

GreyGnome

OOps, drat- I got a typo and confused the issue.  I will ask this in the programming forum section.

I don't know why people think you can't define classes in the sketch.  I do it all the time, with no issues.

pYro_65

Quote
I don't know why people think you can't define classes in the sketch.  I do it all the time, with no issues.


Try that version of code I posted.
The single file wont compile, but the separated version does.

the only difference in code is

#include <Arduino.h>
and
#include "myclass.h"

If you know why this occurs, please post as I know this is not normal behaviour.

Jantje

Pyro
Thanks for this info. Indeed it doesn't work. It has to do with the fact that arduino IDE allows you to "ignore C++ rules"
I copied below the C++ code Arduino IDE makes from your code
Code: [Select]
#include "Arduino.h"
void reportit(myclass &i);
void setup();
void loop();
class myclass
{
  public:
 
  void yo() {
    Serial.println("Hello");
  }
};

void reportit(myclass &i) { //cant make up class names pbs??
  i.yo(); // If this didn't give an error, I would uncomment this method call.
  Serial.print("test");
};

void setup()
{
  Serial.begin(115200); Serial.println("---------------------------------------");
};

void loop() {
 
  myclass m_Class; //Class instance
  reportit( m_Class ); //Pass to function
};


As you can see the method reportit is inserted before the class definition.
This is causing compiler errors in "unseen code"
The same is probably true for structures or function declarations.
The work around you proposed is good.

Best regards
Jantje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Go Up