I have been tearing my hair out :0 on this one all day. I am having problems finding the exact syntax for a struct passed to a function, and actually get my friggin' sketch to compile. I have been digging thru these forums and some C++ forums and followed all the examples precisely but it won't compile.
I've seen several different examples of structs, with different syntaxes, but none of them will compile when I have a function that uses it. I do need to be able to pass the struct to a function. And in the future, I may want the ability to return it. Apparently this problem is due to the autogeneration of prototypes. Some of the workarounds I've read say I need to put it into an .h file, or .cpp, or .pde, which I have tried. I've also tried turning it into a class without any luck. I've tried to pass it with a pointer but couldn't figure out a working syntax for that either.
I trimmed my project down to a simple test sketch, below. As posted, it will compile. But if you unremark the isHoliday() function, it returns an error: 'dateTime' was not declared in this scope and several others starting with the definition of the struct.
#define myBirthday 7
#define Christmas 5
#define NewYears 11
/*
struct dateTime {
byte month;
byte dayOfMonth;
int year;
};
dateTime curDT = {6,22,1963};
// The code above compiles IF I get rid of the isHoliday() function
// but gives error: 'dateTime' was not declared in this scope
// on line 8 with the isHoliday() function unremarked
/*
// These don't work, gives error: 'curDT' does not name a type
// so how do I get or alter a value from one of it's elements?
//curDT.Month = 8;
//curDT->month = 8;
*/
//Also tried it as a class, like this:
class dateTime {
public:
byte month;
byte dayOfMonth;
int year;
dateTime();
};
dateTime::dateTime() {
this->month = 1;
this->dayOfMonth = 1;
this->year = 1970;
}
dateTime curDT();
/*
// These don't work with the class either, also gives error: 'curDT' does not name a type
// so how do I get or alter a value from one of it's elements?
//curDT.Month = 8;
//curDT->month = 8;
*/
int holidayList[] = { 0,0,0,0,0,0,0,0,0,0 }; // Maximum of 10 holidays or events on a single day
int holidayCount = 0;
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
} // void setup
void loop()
{
// put your main code here, to run repeatedly:
int result;
//result = isHoliday(curDT, holidayList);
Serial.print ("Holiday Count = ");
Serial.println (holidayCount);
Serial.print ("Holidays: ");
for (int i=0; i<holidayCount; i++) {
Serial.print (holidayList[i]);
Serial.print (", ");
} // for holiday loop
} // void loop()
/*
int isHoliday(dateTime dt, int holidays[])
// causes error: 'dateTime' was not declared in this scope
// on line 8 above with the isHoliday() function not remarked out
{
int numHolidays=0;
addHoliday(holidays, numHolidays, myBirthday);
addHoliday(holidays, numHolidays, Christmas);
return numHolidays;
} // int isHoliday
*/
boolean addHoliday(int holidays[], int &numHolidays, int newHoliday)
{
if (numHolidays < 10) {
holidays[numHolidays] = newHoliday;
numHolidays++;
return true;
} // if (curCount < 10)
else {
return false;
} // else if (curCount < 10)
} // boolean addHoliday
My real struct is much larger, with more elements, so it is impractical to pass them all as separate parameters to the function.
The isHoliday() function is also much more complex, checking the elements of the structure for a match.
P.S. I'm a C# programmer by day. Just similar enough, and yet just different enough to drive me nuts. ]