Hi,
I am trying to give an object as an argument to a function, but I am getting an error:
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
Should move this too the programming questions forum.
pbs is not an object... easier to show code fixes.
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 )
#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
}
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
#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.