1

I made this simple code but I'm getting the following error

exit status 1
'RGB' does not name a type
typedef struct
RGB
{
        double r;
        double g;
        double b;
};

RGB led;

RGB func(){
  RGB temp;
  return RGB;
}

I am not getting the error on "RGB led" but just on the function. Does anyone know a solution for this? Thanks!

1
  • The typedef syntax is "typedef [known type] [new name];" so RGB would go after the closing brace of the structure definition. Or see @edgarbonet's post for another alternative.
    – JRobert
    Mar 12 '20 at 15:11
4

In plain C, structs are identified by "struc tags", which do not live in the same namespace as type names. Thus, if you declare

struct RGB {
    ...
};

a variable of this type should be declared as

struct RGB led;

In order to avoid the inconvenience of repeatedly typing struct, a common idiom is to define it as a type:

typedef struct {
    ...
} RGB;

RGB led;

The Arduino environment, however, is based on the C++ programming language. In C++ struct tags are types, and you thus do not need the typedef:

struct RGB {
    ...
};

RGB led;

This is the recommended idiom in C++: just remove the typedef.

1

There are 2 errors:

you are returning a class type, and not the temp object

  RGB func(){
    RGB temp;
    return RGB; --> return temp;
  }

To use your typedef you should put the struct "name" after the braced parenthesis:

typedef struct
{
    double r;
    double g;
    double b;
} RGB;
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.