[Solved ] "String function was not declared in this scope"

Hello !

I am trying to return a String from a function but I keep getting the error saying that " 'my function' was not declared in this scope"

Here is a little example emulating what is happening with my code:

void makeLog() {
  String string = logPath(); // error: `logPath` was not declared in this scope
}

String logPath() {
  String currentPath = "";
  currentPath = "/logFile.txt";
  return currentPath;
}

What am I doing wrong ? How am I supposed to use these functions ?

Thank you !

What am I doing wrong ?

We need to see a complete program that demonstrates the problem, not just a snipppet

UKHeliBob:
We need to see a complete program that demonstrates the problem, not just a snipppet

That's the whole code... I've just begun writing this code and I stumbled upon the error I mentioned above.

#include <Arduino.h>
#include <SPIFFS.h>

void makeLog() {
  String string = logPath(); // error: `logPath` was not declared in this scope
}

String logPath() {
  String currentPath = "";
  currentPath = "/logFile.txt";
  return currentPath;
}

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Output:

src/main.cpp: In function 'void makeLog()':
src/main.cpp:5:27: error: 'logPath' was not declared in this scope
String string = logPath(); // error: `logPath` was not declared in this scope
^
*** [.pioenvs/esp32doit-devkit-v1/src/main.cpp.o] Error 1

Your code compiles fine for me using Arduino IDE 1.8.9. Are you using PlaformIO?

The problem is most likely a failure to insert the function prototype for logPath() in the correct location, above makeLog(). A function must be declared before the first time it is referenced in your program. The Arduino IDE automatically generates function prototypes in an attempt to provide a more gentle learning curve for beginners (and, even as someone who fully understands prototypes, I still find this feature very convenient). This automatic prototype generation turns out to be quite difficult to get right every time. The Arduino IDE has gotten better at it over the years to the point where it rarely causes problems.

The workaround for a function prototype generation failure is to simply add your own prototype in the correct place. The Arduino IDE will not generate a prototype when one already exists in your code. I'm sure PlatformIO is the same.

#include <Arduino.h>
#include <SPIFFS.h>

String logPath();

void makeLog() {
  String string = logPath(); // error: `logPath` was not declared in this scope
}

String logPath() {
  String currentPath = "";
  currentPath = "/logFile.txt";
  return currentPath;
}

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

pert:
Your code compiles fine for me using Arduino IDE 1.8.9. Are you using PlaformIO?

The problem is most likely a failure to insert the function prototype for logPath() in the correct location, above makeLog(). A function must be declared before the first time it is referenced in your program. The Arduino IDE automatically generates function prototypes in an attempt to provide a more gentle learning curve for beginners (and, even as someone who fully understands prototypes, I still find this feature very convenient). This automatic prototype generation turns out to be quite difficult to get right every time. The Arduino IDE has gotten better at it over the years to the point where it rarely causes problems.

The workaround for a function prototype generation failure is to simply add your own prototype in the correct place. The Arduino IDE will not generate a prototype when one already exists in your code. I'm sure PlatformIO is the same.

#include <Arduino.h>

#include <SPIFFS.h>

String logPath();

void makeLog() {
  String string = logPath(); // error: logPath was not declared in this scope
}

String logPath() {
  String currentPath = "";
  currentPath = "/logFile.txt";
  return currentPath;
}

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Thank you for your answer !

Yes, I am using PlatformIO.

I am ashamed that I didn't think of changing their order. It works now !

Thank you for the quick fix.

That's the whole code...

Well, as it turns out it wan't was it ?