"function definition is not allowed here"


Keeps saying function definition is not allowed here on curly brackets I didn’t place. Also says it is expecting one in a spot where there is one. Have tried removing and replacing all of them in different ways. The closing curly brackets do not have errors.

Do note, I did not type in any of these curly brackets, nor any other ones, they were put in by the format. This has not been a problem in any other program.

Usually this means you’re defing a function inside of another one. Can you post the entire code?

You probably have another function above those you did write, and you don’t have a closing curly bracket on it. This would make it think you wrote the other functions inside of that function, and when it reaches the end of the file it realized that it never found a closing curly bracket.

#pragma region VEXcode Generated Robot Configuration
// Make sure all required headers are included.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>


#include "vex.h"

using namespace vex;

// Brain should be defined by default
brain Brain;


// START V5 MACROS
#define waitUntil(condition)                                                   \
  do {                                                                         \
    wait(5, msec);                                                             \
  } while (!(condition))

#define repeat(iterations)                                                     \
  for (int iterator = 0; iterator < iterations; iterator++)
// END V5 MACROS


// Robot configuration code.
controller Controller1 = controller(primary);
motor leftMotorA = motor(PORT16, ratio18_1, false);
motor leftMotorB = motor(PORT15, ratio18_1, false);
motor_group LeftDriveSmart = motor_group(leftMotorA, leftMotorB);
motor rightMotorA = motor(PORT14, ratio18_1, true);
motor rightMotorB = motor(PORT11, ratio18_1, true);
motor_group RightDriveSmart = motor_group(rightMotorA, rightMotorB);
drivetrain Drivetrain = drivetrain(LeftDriveSmart, RightDriveSmart, 319.19, 295, 40, mm, 1);

motor rollertest = motor(PORT7, ratio18_1, false);

motor flywheel = motor(PORT4, ratio18_1, false);

motor intakemoment = motor(PORT3, ratio18_1, false);

led endgame = led(Brain.ThreeWirePort.D);



// define variable for remote controller enable/disable
bool RemoteControlCodeEnabled = true;
// define variables used for controlling motors based on controller inputs
bool Controller1LeftShoulderControlMotorsStopped = true;
bool Controller1RightShoulderControlMotorsStopped = true;
bool Controller1UpDownButtonsControlMotorsStopped = true;
bool DrivetrainLNeedsToBeStopped_Controller1 = true;
bool DrivetrainRNeedsToBeStopped_Controller1 = true;

// define a task that will handle monitoring inputs from Controller1
int rc_auto_loop_function_Controller1() {
  // process the controller input every 20 milliseconds
  // update the motors based on the input values
  while(true) {
    if(RemoteControlCodeEnabled) {
      
      // calculate the drivetrain motor velocities from the controller joystick axies
      // left = Axis3 + Axis1
      // right = Axis3 - Axis1
      int drivetrainLeftSideSpeed = Controller1.Axis3.position() + Controller1.Axis1.position();
      int drivetrainRightSideSpeed = Controller1.Axis3.position() - Controller1.Axis1.position();
      
      // check if the value is inside of the deadband range
      if (drivetrainLeftSideSpeed < 5 && drivetrainLeftSideSpeed > -5) {
        // check if the left motor has already been stopped
        if (DrivetrainLNeedsToBeStopped_Controller1) {
          // stop the left drive motor
          LeftDriveSmart.stop();
          // tell the code that the left motor has been stopped
          DrivetrainLNeedsToBeStopped_Controller1 = false;
        }
      } else {
        // reset the toggle so that the deadband code knows to stop the left motor nexttime the input is in the deadband range
        DrivetrainLNeedsToBeStopped_Controller1 = true;
      }
      // check if the value is inside of the deadband range
      if (drivetrainRightSideSpeed < 5 && drivetrainRightSideSpeed > -5) {
        // check if the right motor has already been stopped
        if (DrivetrainRNeedsToBeStopped_Controller1) {
          // stop the right drive motor
          RightDriveSmart.stop();
          // tell the code that the right motor has been stopped
          DrivetrainRNeedsToBeStopped_Controller1 = false;
        }
      } else {
        // reset the toggle so that the deadband code knows to stop the right motor next time the input is in the deadband range
        DrivetrainRNeedsToBeStopped_Controller1 = true;
      }
      
      // only tell the left drive motor to spin if the values are not in the deadband range
      if (DrivetrainLNeedsToBeStopped_Controller1) {
        LeftDriveSmart.setVelocity(drivetrainLeftSideSpeed, percent);
        LeftDriveSmart.spin(forward);
      }
      // only tell the right drive motor to spin if the values are not in the deadband range
      if (DrivetrainRNeedsToBeStopped_Controller1) {
        RightDriveSmart.setVelocity(drivetrainRightSideSpeed, percent);
        RightDriveSmart.spin(forward);
      }
      // check the ButtonL1/ButtonL2 status to control rollertest
      if (Controller1.ButtonL1.pressing()) {
        rollertest.spin(forward);
        Controller1LeftShoulderControlMotorsStopped = false;
      } else if (Controller1.ButtonL2.pressing()) {
        rollertest.spin(reverse);
        Controller1LeftShoulderControlMotorsStopped = false;
      } else if (!Controller1LeftShoulderControlMotorsStopped) {
        rollertest.stop();
        // set the toggle so that we don't constantly tell the motor to stop when the buttons are released
        Controller1LeftShoulderControlMotorsStopped = true;
      }
      // check the ButtonR1/ButtonR2 status to control intakemoment
      if (Controller1.ButtonR1.pressing()) {
        intakemoment.spin(forward);
        Controller1RightShoulderControlMotorsStopped = false;
      } else if (Controller1.ButtonR2.pressing()) {
        intakemoment.spin(reverse);
        Controller1RightShoulderControlMotorsStopped = false;
      } else if (!Controller1RightShoulderControlMotorsStopped) {
        intakemoment.stop();
        // set the toggle so that we don't constantly tell the motor to stop when the buttons are released
        Controller1RightShoulderControlMotorsStopped = true;
      }
      // check the ButtonUp/ButtonDown status to control flywheel
      if (Controller1.ButtonUp.pressing()) {
        flywheel.spin(forward);
        Controller1UpDownButtonsControlMotorsStopped = false;
      } else if (Controller1.ButtonDown.pressing()) {
        flywheel.spin(reverse);
        Controller1UpDownButtonsControlMotorsStopped = false;
      } else if (!Controller1UpDownButtonsControlMotorsStopped) {
        flywheel.stop();
        // set the toggle so that we don't constantly tell the motor to stop when the buttons are released
        Controller1UpDownButtonsControlMotorsStopped = true;
      }
    }
    // wait before repeating the process
    wait(20, msec);
  }
  return 0;
}

task rc_auto_loop_task_Controller1(rc_auto_loop_function_Controller1);
#pragma endregion VEXcode Generated Robot Configuration

// Include the V5 Library
#include "vex.h"
  
// Allows for easier use of the VEX Library
using namespace vex;

competition Competition;

float myVariable;

// "when autonomous" hat block
int onauton_autonomous_0() {
   // auton-fix-work
  // apparently works? new tests + rollerchange needed
  rollertest.setVelocity(100.0, percent);
  intakemoment.setVelocity(100.0, percent);
  flywheel.setVelocity(100.0, percent);
  Drivetrain.setDriveVelocity(100.0, percent);
  Drivetrain.setTurnVelocity(100.0, percent);
  Drivetrain.setStopping(coast);
  Drivetrain.driveFor(forward, 15, inches);
  Drivetrain.turnFor(right, 90, degrees);
  Drivetrain.driveFor(forward, 5, inches);
  rollertest.spinFor(forward, 90, degrees);
  // velocity tweak/less drive? 
  return 0;
}

// "when driver control" hat block
int ondriver_drivercontrol_0() {
   //tweak velocity?
  Drivetrain.setTurnVelocity(100.0, percent);
  Drivetrain.setDriveVelocity(100.0, percent);
  intakemoment.setVelocity(100.0, percent);
  flywheel.setVelocity(100.0, percent);
  rollertest.setVelocity(100.0, percent);
  Drivetrain.setStopping(coast);
  //ghengis-notcode
  Brain.Screen.drawCircle(0, 0, 10);
  Brain.Screen.setPenColor(red);
  Brain.Screen.setFont(prop60);
  Brain.Screen.print("Nice");
  Brain.Screen.setPenColor(yellow);
  Brain.Screen.print("Going,");
  Brain.Screen.newLine();
  Brain.Screen.setPenColor(green);
  Brain.Screen.print("Ghengis");
  Brain.Screen.newLine();
  Brain.Screen.setPenColor(cyan);
  Brain.Screen.print("!!");
  Brain.Screen.newLine();
  Brain.Screen.setPenColor(white);
  Brain.Screen.print("Roger moment");
  //pneumatics
      while (true) {
    if (Controller1.ButtonX.pressing()){
      endgame.on();
    }
    else {
      endgame.off();
    }
  wait(5, msec);
  return 0;

void VEXcode_driver_task() {
  // Start the driver control tasks....
  vex::task drive0(ondriver_drivercontrol_0);
  while(Competition.isDriverControl() && Competition.isEnabled()) {this_thread::sleep_for(10);}
  drive0.stop();
  return;
}

void VEXcode_auton_task() {
  // Start the auton control tasks....
  vex::task auto0(onauton_autonomous_0);
  while(Competition.isAutonomous() && Competition.isEnabled()) {this_thread::sleep_for(10);}
  auto0.stop();
  return;
}



int main() {
  vex::competition::bStopTasksBetweenModes = false;
  Competition.autonomous(VEXcode_auton_task);
  Competition.drivercontrol(VEXcode_driver_task);

  // post event registration

  // set default print color to black
  printf("\033[30m");

  // wait for rotation sensor to fully initialize
  wait(30, msec);
}

I fixed the problem by deleting the ending part and replacing it with its twin code (This code is for Blue, I used the rest from Red, will edit it slightly so it moved the correct direction of course)
But I still don’t know the issue, and it would be good to know for future reference and/or viewers of the post.

wait why is there 2 using namespace in the code and 2 #include “vex.h”?

1 Like

You are missing the close curly-brace } on the function int ondriver_drivercontrol_0(). It should be placed after the line that says return 0;.

3 Likes

I realized that like 2 seconds after posting. But still, it didn’t change anything.

I don’t know. I didn’t edit the pragma statement or anything before the autonomous hat block.

Yep, that’s definitely where it’s missing.

This is exactly why I harp on new SW devs to comment at the end of their braces as to what scope is ending (if, while, switch, function, etc.). Makes it much easier to look at a long block of code and keep track of scope (and when you’re missing that, it shows up much faster).

as said, i added it, and it didn’t change anything.

That’s because you are missing two braces. it should be.

      endgame.off();
    }
    wait(5, msec);
  }
  return 0;
}

fixing all the indenting would help identify these types of issues.

5 Likes