a function-definition is not allowed here before '{' token ERROR

Hi,

I had a program which was working perfectly till a while back and then I got an error which said “function not defined in scope”. I then added all function declaration prototypes before the setup(). Now, a new error has popped up in the most basic of functions I have in my program.
Please help me understanding what is happening. The code is below -

void dashedLine()
{
 for (int line = 110; line > 0; line--)
 {
  Serial.print('-');
 }
 Serial.println();
}

Well you only posted a snippet, but if I put it below loop() and call it from setup() as below it compiles for me on a Uno.

void setup() {
dashedLine();
}

void loop() {
}


void dashedLine()
{
 for (int line = 110; line > 0; line--)
 {
  Serial.print('-');
 }
 Serial.println();
}

Presumably you have more code :wink:

manor_royal:
Well you only posted a snippet, but if I put it below loop() and call it from setup() as below it compiles for me on a Uno.

Presumably you have more code :wink:

I do, I have close to 15 other functions. This code compiled perfectly till a while back and I didn't change anything in this function. I have the functions in different tabs. What seems to be the mistake?

You said there is a new error, but what is it? Is that the error in the title?

My code compiles and draws a line (after I remembered to add a Serial.begin anyway), and I'm not going to speculate about where your mistake is if you don't post the code.

Arduino: 1.6.10 (Windows 7), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\abhilash.r\Documents\Arduino\Cooksmart_flow_feb11o\dashedLine.ino: In function 'void cook(char)':

dashedLine:6: error: a function-definition is not allowed here before '{' token

{

^

whistleLoop:14: error: expected '}' at end of input

}

^

exit status 1
a function-definition is not allowed here before '{' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

These are the errors. =(

manor_royal:
You said there is a new error, but what is it? Is that the error in the title?

My code compiles and draws a line (after I remembered to add a Serial.begin anyway), and I’m not going to speculate about where your mistake is if you don’t post the code.

Yep. The new error is the one in the title. I did add Serial.begin in the setup().

And, about posting the whole code, I am not sure which parts are necessary. This is the function which is called in some places to make the serial output data more readable.

void setup() 
{
 Serial.begin(9600); //Initialize TX0/RX0
 Serial1.begin(9600); //Initialize TX1/RX1
 Serial.println("<CookSmart test program>");
 Serial.println();
 Serial.println("Initializing...");
 Serial.println();
 pinMode(LED, OUTPUT); //Declared LED as output
 pinMode(encoderA, INPUT); pinMode(encoderB, INPUT); pinMode(encoderC, INPUT); pinMode(encoderD, INPUT); //Declared all encoders as input ports
 pinMode(LSA, INPUT_PULLUP); pinMode(LSB, INPUT_PULLUP); pinMode(LSC, INPUT_PULLUP); pinMode(LSD, INPUT_PULLUP); //Declared all limit switches as input ports (used in-built pullup resistors)
 pinMode(m11, OUTPUT); pinMode(m12, OUTPUT); pinMode(m21, OUTPUT); pinMode(m22, OUTPUT); pinMode(m31, OUTPUT); pinMode(m32, OUTPUT); pinMode(m41, OUTPUT); pinMode(m42, OUTPUT); //Declared all motor pins as output ports

 sysReset(LSA, encoderA, m11, m12, 1, encMemA);

 dashedLine();
}

Hi,

C:\Users\abhilash.r\Documents\Arduino\Cooksmart_flow_feb11o\dashedLine.ino: In function 'void cook(char)':

Look for problem in void cook(char) function for a start.

Tom..... :slight_smile:

TomGeorge:
Hi,

Look for problem in void cook(char) function for a start.

Tom..... :slight_smile:

You mean to say the error is in the void cook(char) function? I will have a look.

Thanks guys!
I was missing a '}' in the cook function of my program. :-\

Imagine that! The error message told you what the error was :wink:

Like pulling teeth.

ChrisTenone:
Like pulling teeth.

...but not quite as much fun

manor_royal:
Imagine that! The error message told you what the error was :wink:

I kept thinking that the error was in the dashedLine function... My bad!

(deleted)

abhir24:
I do, I have close to 15 other functions. This code compiled perfectly till a while back and I didn't change anything in this function. I have the functions in different tabs. What seems to be the mistake?

You changed something before the function, and that has confused the compiler.

Let's take the sentence "Batman scaled the building." It's pefectly fine. But if I put "Superman flew over " in front of it - accidentally missing "the tower, and " - not only is the sentence unparseable but it's not exactly clear where the problem is. The first thing you hit that can't be parsed is 'scaled' and that's where the compiler will report the error, even though the sentence 'Batman scaled the building' hasn't changed.

PaulMurrayCbr:
You changed something before the function, and that has confused the compiler.

Let's take the sentence "Batman scaled the building." It's pefectly fine. But if I put "Superman flew over " in front of it - accidentally missing "the tower, and " - not only is the sentence unparseable but it's not exactly clear where the problem is. The first thing you hit that can't be parsed is 'scaled' and that's where the compiler will report the error, even though the sentence 'Batman scaled the building' hasn't changed.

Thank you, Paul for the detailed explanation. I now understand where the error crept in!

And, about posting the whole code, I am not sure which parts are necessary.

Look at that statement. The WHOLE code means ALL of the code. There are NO "parts" of THE WHOLE CODE.