A function definition is not allowed here before C++

#include <Wire.h>

#include <SoftwareSerial.h>

#define RE 8
#define DE 7
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
const byte nitro[] = {0x01,0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
const byte phos[] = {0x01,0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc};
const byte pota[] = {0x01,0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};
SoftwareSerial espSerial(5, 6);
int nitrogen();
int phosphorous();
int potassium();
String str;
byte values[11];
SoftwareSerial mod(2,3);
const int PUMP1 = A0;
const int PUMP2 = A1;
const int SOLENOID1 = A2;
const int SOLENOID2 = A3;
byte val1,val2,val3;

void setup(){
Serial.begin(115200);
espSerial.begin(115200);

pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
pinMode(PUMP1, OUTPUT);
pinMode(PUMP2, OUTPUT);
pinMode(SOLENOID1, OUTPUT);
pinMode(SOLENOID2, OUTPUT);


}
void loop()
{



str =String(values[11]);
espSerial.println(str);


val1 = nitrogen();
delay(250);
val2 = phosphorous();
delay(250);
val3 = potassium();
delay(250);


if(val1,val2,val3<=20)
 {
  digitalWrite(SOLENOID1, LOW); // turn on solenoid2 10 seconds
  digitalWrite(PUMP2, LOW); // turn on pump2 7 seconds
 }
if(val1,val2,val3>20)
 {
  digitalWrite(SOLENOID2, HIGH); // turn on solenoid2 10 seconds
  digitalWrite(PUMP2, HIGH);  // turn off pump2 7 seconds
 }

                 

byte nitrogen(){
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(mod.write(nitro,sizeof(nitro))==8){
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
for(byte i=0;i<7;i++){
//Serial.print(mod.read(),HEX);
values[i] = mod.read();
Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4];
}
 
byte phosphorous()
{
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
if(mod.write(phos,sizeof(phos))==8)
{
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);

for(byte i=0;i<7;i++)
{
//Serial.print(mod.read(),HEX);
values[i] = mod.read();
Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4];
}
 

byte potassium(){
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
if(mod.write(pota,sizeof(pota))==8){
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
for(byte i=0;i<7;i++){
values[i] = mod.read();
Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4];
}                     
}

error

C:\Users\Admin\Documents\Arduino\ssr3\ssr3.ino: In function 'void loop()':
ssr3:71:16: error: a function-definition is not allowed here before '{' token
 byte nitrogen(){
                ^
ssr3:89:1: error: a function-definition is not allowed here before '{' token
 {
 ^
ssr3:109:17: error: a function-definition is not allowed here before '{' token
 byte potassium(){
                 ^
exit status 1
a function-definition is not allowed here before '{' token

A function definition is not allowed here before C++

้ำะ่้พะ่พ1110×843 30.3 KB

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

Hi all,
I have this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include 

#define func( returnType, name, params, body ) { \
    returnType name params { \
        body; \
    } \
}    

int main( int argc, char* argv[] ) {
    func( int, Add, ( int a, int b ), {
        return a + b;
    } );

    std::cout << Add( 5, 10 ) << std::endl;

    return 0;
}

But it gives the error in the title:
Line 13: error: a function-definition is not allowed here before '{' token
What is wrong?
Thanks!

You can't put the function definition inside another function ( main() is a function).

1. you can't define functions inside another function. you can call them, but not define them there.

2.

1
2
3
4
5
func( int, Add, ( int a, int b ), {
        return a + b;
    } );

    std::cout << Add( 5, 10 ) << std::endl;

you don't define the function "Add" like this

what you want is:

1
2
3
4
int Add(int a, int b)
{
   return a + b;
}

You can't put the function definition inside another function ( main() is a function)
Ah, of course, I hadn't thought of that. Thanks!

@darkmaster: yeah, I know, but I am fascinated by define's and so I'm experimenting with them :)

Topic archived. No new replies allowed.

Hello guys, im desperate, I dont know how these kind of errors are even fair:

Line 114 a function definition is not allowed here before ‘}’ token
Line 124 a function definition is not allowed here before ‘}’ token
Line 129 a function definition is not allowed here before ‘}’ token
Line 152 expected ‘}’ at end of input

I put the error lines in bold with a triple $$$

errorerror.PNG770x145 5.96 KB

#include 
#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

#define ARRIBA 72
#define IZQUIERDA 78
#define DERECHA 77
#define ABAJO 80
#define ESC 27

int cuerpo [200] [2];
int n = 1;
int tam = 4;
int x = 10, y = 12;
int dir = 7;
int xc = 30, yc = 15;

char tecla;

void gotoxy (int x, int y)
{
HANDLE hCon;
COORD dwPos;

dwPos.X = x;
dwPos.Y = y,
hCon = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition (hCon, dwPos);
}

void pintar()
{
// lineas horizontales
for (int i = 2 ; i < 78 ; i++)
{
gotoxy (i,3); printf("%c", 205);
gotoxy (i,23); printf("%c", 205);

}
// lineas verticales
for (int i = 4 ; i < 23 ; i++)
  {
  	gotoxy  (2,i); printf("%c",186);
  	gotoxy (77,i); printf("%c",186);
  	 
}
// esquinas
{
  	gotoxy  (2,3); printf("%c", 201);
  	gotoxy (2,23); printf("%c", 200);
	gotoxy  (77,3); printf("%c", 287);
  	gotoxy (77,23); printf("%c", 188);	  
}
}

void guardar_posicion()
{
cuerpo[n][0] = x;
cuerpo[n][1] = y;
n++;
if (n == tam) n = 1;
}

void dibujar_cuerpo()
{
for (int i = 1 ; i < tam ; i++)
{
gotoxy(cuerpo[i][0], cuerpo[i][1] ); printf("*");
}	
}

void borrar_cuerpo()
{

	gotoxy (cuerpo[n][0], cuerpo [n][1] ); printf(" ");
}

void teclear()
{

	if (kbhit())
	{
		tecla = getch ();
		switch (tecla)
		 {
			case ARRIBA :
				if (dir != 2)
				dir = 1;
				break;
			case ABAJO :
				if (dir != 1)
			 	dir = 2;
				 break;	
			case DERECHA :
				if (dir != 4)
				dir = 3;
				break;
			case IZQUIERDA :
			 	if (dir != 3)
				 dir = 4;
				 break;	
		 }	
}

void comida()
$$${
if (x == xc && y == yc)
xc = (rand()%73) + 4;
yc = (rand()%19) + 4;

tam ++;
gotoxy(xc. yc); printf("%c", 4); 
}

bool game_over()
$$${
if (y == 3 || y == 23 || x == 2 || x = 77) return true;
return false;
}

$$$int main () {

pintar ();
gotoxy(xc. yc); printf("%c", 4); 

while(tecla != ESC || !game_over())
  {
  	teclear()
	borrar_cuerpo();
	guardar_posicion();
	dibujar_cuerpo();	
	comida ();
	 }
  
 if (dir == 1) y--;
  if (dir == 2) y++;
  if (dir == 3) x++;
  if (dir == 4) x--;

Sleep(100);

system ("pause>null");
return 0;
$$$}

How do you fix error function definition is not allowed here?

You have your classes' function definitions inside your main function, which is not allowed. To fix that, you should place them outside, but to do that you will need to place the whole class outside of main as well (since you need it to be in scope):

Can a function be called before declaration?

Hoisting. With JavaScript functions, it is possible to call functions before actually writing the code for the function statement and they give a defined output. This property is called hoisting. Hoisting is the ability of a function to be invoked at the top of the script before it is declared.

Is it mandatory to declare a function before use in C?

Actually, it is not required that a function be declared before use in C. If it encounters an attempt to call a function, the compiler will assume a variable argument list and that the function returns int.

Can you declare a function and not define it?

Defining a function means providing a function body; defining a class means giving all of the methods of the class and the fields. Once something is defined, that also counts as declaring it; so you can often both declare and define a function, class or variable at the same time. But you don't have to.