I’m having an issue with the spark web IDE – I want to use a custom datatype in my program, and the only way I can do it is to put the datatype’s definition in a separate .h file. This works, but it feels hackish, and it’s a pain to share multi-file programs with other people using the web IDE. I’m looking for a way to declare a custom struct datatype in the same file as my functions. The full details are below. Any advice is welcome!
I’m writing a spark program, and I want to use a custom datatype, so I declare a struct:
typedef struct{
unsigned char red, green, blue;
} color;
I want to use my newly declared color type as a parameter in a function,
void setPixel(int x, int y, int z, color col);
However, if I write an outline of a simple program
typedef struct{
unsigned char red, green, blue;
}color;
void setup()
{
}
void setPixel(int x, int y, int z, color col)
{
//do something here
}
It doesn’t compile in the web IDE. I get the following error:
listener_-_single_file.cpp:4:36: error: 'color' has not been declared
void setPixel(int x, int y, int z, color col);
I looked in the spark forums, and I found a mention of defining the struct in a separate .h file. So, I adjusted my program to:
#include "colors.h"
void setup()
{
}
void setPixel(int x, int y, int z, color col)
{
//do something here
}
and created a separate colors.h file:
typedef struct{
unsigned char red, green, blue;
} color;
Lo and behold, this works! I don’t know why it has to be this way, though. It feels clunky, and it makes it harder for me to share code, as it’s no longer a simple copy-and-paste-into-the-IDE operation.
I’m looking for a way to keep my struct definition in the main file, or at least understand why I can’t. Is this something funny about c++ that I don’t understand? Is this a quirk with the web IDE? What gives?