Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By rocket_man
#38739 I'd like to preface this by saying I'm new to C++ and ESP8266 programming, so please don't laugh too hard if it's a stupid question :D

I noticed that when I include the ESP8266WiFi.h in a sketch, it forces the use of function declarations, otherwise I get a "not declared in this scope" error.

Here's a minimum code example:

This compiles just fine
Code: Select all#include <Ethernet.h>

void setup() {
  Serial.begin(9600);
  testFunction();
}

void loop() {
}

void testFunction()
{
  Serial.println("test");
}


This on the other hand, gives a compilation error complaining that testFunction was not declared in the scope
(error: 'testFunction' was not declared in this scope)
Code: Select all#include <ESP8266WiFi.h>

void setup() {
  Serial.begin(9600);
  testFunction();
}

void loop() {
}

void testFunction()
{
  Serial.println("test");
}


Therefore, if I include the ESP8266WiFi.h I need to add the function declaration "void testFunction();" somewhere before the setup() function.

So the question is why does including the ESP8266WiFi.h have this effect?

Thanks!
User avatar
By WereCatf
#38742 I can't say why including ESP8266WiFi.h does that, but having to either declare a function or having the function-code itself appear before it's called is ANSI-standard C++ and thus it's a good habit to learn anyways -- if you ever decide to code in C/C++ in a non-Arduino environment you'd run into the exact same thing. In your example, either move the testFunction() itself above setup() or, indeed, declare the function there first, and just learn to live with it :)
User avatar
By rocket_man
#38820 Ah, I see - thank you both for the info. As WereCatf suggested, I'll just stick to declaring functions as the C++ standards stipulate. That way the code will work no matter what happens with the Arduino IDE, and I'll learn to use good C++ coding practices.

As jmehan mentioned, the change is indeed causing problems. The way I came across the problem is by trying to use an example ESP8266 sketch from somebody's github and it wouldn't compile. Same thing - errors about functions not being declared in this scope fixed with function declarations.