Function to return custom Data Type

I have been able to write functions and pass, as arguments, custom data types both as objects and as pointers.

I have looked for examples where I pass simple data types such as long, integer etc but the function itself returns a custom type but no luck. I am beginning to wonder if this can be done.

For example, I declare a class or a structure or even a typedef structure:

struct myNewType
{
  int a;
  double b;
};

or

class myNewType
{
  public:
    int a;
    double b;
};

or

typedef struct {
  int a;
  double b;
}  myNewType;

Each time I try to construct a function by declaring the new type I get 'myNewType' does not name a type.

The new type is declared in a library.h files and the include is at the beginning of the sketch so I don't think it's a case of the new type not being picked up, rather that my understanding of how it works is a bit mangled.

Where am I going wrong?

Ric

You will need to put the struct in a header file.

Create a tab called
whateverYouWantToCallIt.h

And then put the struct in there.

Yep, done that - although it was not expressed as clearly as it could have been:

The new type is declared in a library.h file and the include is at the beginning of the sketch so I don't think it's a case of the new type not being picked up

Ric

Strange, is it ( library.h ) in the sketch folder?
Did you restart the IDE so the tab for it appears?

Did you use #include "library.h" or #include <library.h>, for sketch folder you want "" not <> version.

If its not one of these problems, you're gonna have to post your library.h contents and your sketch contents.
Also, try putting a forward declaration of the class/struct above the #include line.

Actually you were closer to the mark than I ever thought.

If I embed the struct near the beginning of the sketch it compiles OK. Therefore the struct is OK. The problem is therefore to do with accessing the header file. REmoving the struct from the sketch allows me to check how the header file is accessed.

For the include I had, for example:

#include <mylib.h>

Changing this to:

#include "mylib.h"

solved the problem (mylib.h was in the same folder as the sketch, accessed through the secondary tab in the IDE).

Creating a copy of mylib.h and placing it inside the Arduino libraries folder also failed:

#include <mylib.h>

But then, saving, closing and re-opening the sketch gave no compile errors at all. I have noticed this before when first establishing libraries. Seems like the IDE needs a clean start.

Anyway... problem solved!

Ric

pYro_65, our posts crossed.

Thanks for your suggestions - you were spot on! The voice of experience :slight_smile:

Ric