3rd TAB won't see that Serial has been initialized in main TAB

I have code divided into 3 tabs and I do use prototyping so the complier sees the methods but I can't get the compiler to see that I have initialized Serial in the setup() method. I have clipped code to show the order of events (the code worked until I changed the method command9() from String command9(String data) to void command9().

The complier stops in TAB = commands on the first Serial entry with the error
******* expecting initializer before ‘Serial’ ********

How do I get the complier to see that Serial has been initialized?

i highlight the points of interest with ************** comment ****************
I have 3 TABs
Tab order = simple_Reader + checkRFIDcard + commands
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TAB = simple_Reader
void command9(); ******* method prototype *************
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
void setup()
----- setup all includes --------
Serial.begin(9600);
Serial.println(F("This code scan the MIFARE Classsic NUID."));
Serial.print(F("Using the following key:"));
void loop(){
Serial.print("i = ");
Serial.println(i);
Serial.println(inChar);
case 9: // Master wants to write car data to RFID chip on the car
// statements
command9(); ******* this is the related call ************
prefix = "-";
prefix.concat(reply);
reply = prefix;
reply.concat("\n"); // terminate reply
Serial.print("reply = ");
Serial.println(reply);
sendReply(reply);
break;
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TAB = commands
void command9()
***** this if first line in method ******
--->> Serial.println("got to command 9"); ******* expecting initializer before ‘Serial’ ********
Serial.print("commandData = ");
Serial.println(commandData);
Serial.println(commandData.length());

I think you've thrown away too much of the sketch.

void command9()
              ***** this if first line in method ******
 --->>   Serial.println("got to command 9"); ******* expecting initializer before 'Serial' ********
              Serial.print("commandData = ");

Does your actual sketch include the required '{' between "void command9()" and "Serial.println("got to command 9");"? If not, that is a mistake.

johnwasser:
I think you've thrown away too much of the sketch.

void command9()

***** this if first line in method ******
--->>   Serial.println("got to command 9"); ******* expecting initializer before 'Serial' ********
             Serial.print("commandData = ");




Does your actual sketch include the required '{' between "void command9()" and "Serial.println("got to command 9");"? If not, that is a mistake.

Wow!
johnwasser that was a real find! I feel foolish ..... but that fixed it. Thanks

The compiler has no idea what order things happen at runtime like this, as its equivalent
to solving the halting problem. Should you have forgotten to call Serial.begin() the compiler
would never have warned you.

"Before" in a compiler error always means "previously in the source".

I think I see the source of the original confusion. The error message "expecting initializer before 'Serial'" does NOT mean "You forgot to call 'Serial.begin(baud);' before using 'Serial'." It means "You started declaring a function and at this point (after the ')') you are supposed to put in something called an 'initializer' which, in this context, is either a ';', ending the declaration, or a block statement starting with '{'". "Serial" is neither of those. The error messages is telling you that you are missing something between the previous bit of code and the word "Serial". What you were missing was the '{' that would start the 'initializer'.

It is fairly common to get confusing messages when you have too many or too few brackets:

Common compiler errors caused by mismatched brackets:

"does not name a type" or
"expected declaration before" or
"expected unqualified-id before" or
"expected initializer before" or
"expected constructor, destructor, or type conversion before '(' token"
Usually means you forgot a '{' or put in an extra '}' in the previous function. Since all of the open brackets have been closed, the compiler is looking for further global declarations (variables or functions). If it finds something that looks like executable code instead of a global declaration it emits an error. Make sure that the brackets in the preceding function are in matching pairs '{' followed by '}'.

"a function-definition is not allowed here before '{' token"
(can cause: "'functionName' was not declared in this scope")
Usually means you forgot a '}' or put in an extra '{' in the previous function. Since a set of brackets has not been closed yet the compiler is looking for more code to put in the function. You can't declare a function inside a function so if the compiler finds a function declaration it emits an error. Make sure that the brackets in the preceding function are in matching pairs '{' followed by '}'.

"expected '}' at end of input"
Usually means you forgot a '}' or put in an extra '{' in the last function in the sketch. Since a set of brackets has not been closed yet, the compiler is looking for more code to put in the function. When it hits the end of the file instead, it emits an error. Make sure that the brackets in the last function are in matching pairs '{' followed by '}'.

"expected primary-expression before '}' token"
Usually means you have an incomplete statement before a '}'. The block statement (between '{' and matching '}') can only contain complete statements. Complete statements always end with ';' (or '}' for a block statement).

or a 'FunctionName() is not declared in this scope' Which can be caused by a missing closing '}' at the end of a variable initializer list (or whatever that is called) The IDE then moves the cursor to the spot which it doesn't understand, and usually that is not even remotely close to where the error is. Sometimes you spend hours looking for it. ctrl-T helps, but not always and sometimes makes things worse.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.