user13840: #ifndef FOO_H
#define FOO_H

struct Foo{
  int randInt = random(0, 101);
};

#endif

I have a header file with a struct like above but when I compile I get this error: 
'random' was not declared in this scope
How can I use random() in a header file?

user7856: The struct is a declaration of a type. You cannot mix it with the initialization. This is how it could be:
In foo.h:
#ifndef FOO_H
#define FOO_H

struct Foo{
  int randInt;
};

extern struct Foo foo;
struct Foo *initFoo(struct Foo *foo_ptr);
#endif

In foo.c:
#include "foo.h"

struct Foo foo;
struct Foo *initFoo(struct Foo *foo_ptr) {
  if (!foo_ptr)
    return NULL;
  foo_ptr->randInt = random(0, 101);
  return foo_ptr;
}

random() is a function and therefore its return value cannot be used as static initializer.
Unless -std=c++11 or -std=gnu++11 are enabled.

user13840: Add #include <Arduino.h> like this
#ifndef FOO_H
#define FOO_H

#include <Arduino.h>

struct Foo{
  int randInt = random(0, 101);
};

#endif


13840: <pre><code>#ifndef FOO_H
#define FOO_H

struct Foo{
  int randInt = random(0, 101);
};

#endif
</code></pre>

<p>I have a header file with a <code>struct</code> like above but when I compile I get this error: </p>

<p><code>'random' was not declared in this scope</code></p>

<p>How can I use random() in a header file?</p>
 7856: <p>The struct is a declaration of a type. You cannot mix it with the initialization. This is how it could be:</p>

<p>In foo.h:</p>

<pre><code>#ifndef FOO_H
#define FOO_H

struct Foo{
  int randInt;
};

extern struct Foo foo;
struct Foo *initFoo(struct Foo *foo_ptr);
#endif
</code></pre>

<p>In foo.c:</p>

<pre><code>#include "foo.h"

struct Foo foo;
struct Foo *initFoo(struct Foo *foo_ptr) {
  if (!foo_ptr)
    return NULL;
  foo_ptr-&gt;randInt = random(0, 101);
  return foo_ptr;
}
</code></pre>

<p>random() is a function and therefore its return value cannot be used as static initializer.
Unless -std=c++11 or -std=gnu++11 are enabled.</p>
 13840: <p>Add <code>#include &lt;Arduino.h&gt;</code> like this</p>

<pre><code>#ifndef FOO_H
#define FOO_H

#include &lt;Arduino.h&gt;

struct Foo{
  int randInt = random(0, 101);
};

#endif
</code></pre>
