Error: 'relayState' was not declared in this scope

I’m trying to generate a WebServer for a relay, and i stumbled across an error and i dont know how to fix it. Ive tried moving it out of the for loop but it doesn’t fix it.

String processor(const String& var){
    String buttons ="";
    for(int i=1; i<=NUM_RELAYS; i++){
      String relayStateValue = relayState(i);
      buttons+= "<h4>Relay #" + String(i) + " - GPIO " + relayGPIOs[i-1] + "</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"" + String(i) + "\" "+ relayStateValue +"><span class=\"slider\"></span></label>";
    return buttons;
  return String();

This is the output i get:

src/main.cpp: In function 'String processor(const String&)':
src/main.cpp:59:44: error: 'relayState' was not declared in this scope
       String relayStateValue = relayState(i);

I’m still new to Arduino coding and cpp, so I’m not really sure at what I’m looking for right now

How and where did you declare and define the relayState function?

Since you’re writing a .cpp file and not an Arduino .ino file, all used functions must be declared prior to its use, as the C++ standard demands. Have a look at the FAQ. You might just need to declare

String relayState(int i);

above the function / start of the file.

Ooh ok i see, the function for relayState() was after, so i just switched them, i think its working now! Thank you!

1 Like