Why am I getting an error for my Drive CM Function???


#1

So I’m attempting to make a function for my program to drive a certain number of centimeters using the rotateFor command and I have everything defined but im getting an error that says
15:48:43 – error – error: function definition is not allowed here
void DriveforCm (double DistanceCM, char y) {
^

Why am I getting this error to make the function? Any help would be appreciated

Here’s my code

void DriveforCm (double DistanceCM, int veloc) {
    
        //Define variables
        double wheelDiameterCM  = 10.16;
        double travelTargetCM = DistanceCM; //Wil determine how far the robot goes
    
        //Calc wheel circumferance
        double circumference = wheelDiameterCM * M_PI; 
        double degreesToRotate = (360 * travelTargetCM) / circumference; //All calculations are complete. Start the rest of the program.
    
        //Set the velocity of the left and right motor
        LeftDrive.setVelocity(veloc, vex::velocityUnits::pct); 
        RightDrive.setVelocity(veloc, vex::velocityUnits::pct);
    
        //Rotate the Left and Right Motor for degreesToRotate. 
        LeftDrive.rotateFor(degreesToRotate, vex::rotationUnits::deg);
        RightDrive.rotateFor(degreesToRotate, vex::rotationUnits::deg); 
    
        //Stop motors after reached CM distance
        LeftDrive.stop
        RightDrive.stop
   }   

#2

Are you declaring it inside main or another function?


#3

Yes I’m declaring in


int Main()


#4

You can’t have have function definitions inside other functions

So moving it out of main should fix it


#5

I did that and it didn’t work


#6

can you post all of the code please


#7
vex::competition;

void pre_auton( void ) {
  // All activities that occur before the competition starts
  // Example: clearing encoders, setting servo positions, ...
   void DriveforCM (double DistanceCM, int y) {
    
        //Define variables
        double wheelDiameterCM  = 10.16;
        double travelTargetCM = DistanceCM; //Wil determine how far the robot goes
    
        //Calc wheel circumferance
        double circumference = wheelDiameterCM * M_PI; 
        double degreesToRotate = (360 * travelTargetCM) / circumference; //All calculations are complete. Start the rest of the program.
    
        //Set the velocity of the left and right motor to 50% power. This command will not make the motor spin.
        LeftDrive.setVelocity(y, vex::velocityUnits::pct); 
        RightDrive.setVelocity(y, vex::velocityUnits::pct);
    
        //Rotate the Left and Right Motor for degreesToRotate. 
        LeftDrive.rotateFor(degreesToRotate, vex::rotationUnits::deg); //This command must be non blocking.
        RightDrive.rotateFor(degreesToRotate, vex::rotationUnits::deg); //This command is blocking so the program will wait here until the right motor is done.  
    
        //Stop motors after reached CM distance
        LeftDrive.stop
        RightDrive.stop
   }   
    
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              Autonomous Task                              */
/*                                                                           */
/*  This task is used to control your robot during the autonomous phase of   */
/*  a VEX Competition.                                                       */
/*                                                                           */
/*  You must modify the code to add your own robot specific commands here.   */
/*---------------------------------------------------------------------------*/

void autonomous( void ) {
  // ..........................................................................
  // Insert autonomous user code here.
  // ........................................................................
    
}

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*                              User Control Task                             */
/*                                                                            */
/*  This task is used to control your robot during the user control phase of  */
/*  a VEX Competition.                                                        */
/*                                                                            */
/*  You must modify the code to add your own robot specific commands here.    */
/*----------------------------------------------------------------------------*/

void usercontrol( void ) {
    // User control code here, inside the loop
  while (1)
  {
    vex::task::sleep(20); //Sleep the task for a short amount of time to prevent wasted resources. 
  }
}

//
// Main will set up the competition functions and callbacks.
//
int main() {
    
    //Run the pre-autonomous function. 
    pre_auton();
    
    //Set up callbacks for autonomous and driver control periods.
    Competition.autonomous( autonomous );
    Competition.drivercontrol( usercontrol );

    //Prevent main from exiting with an infinite loop.                        
    while(1) {
      vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
    }    
       
}

#8

You have


void DriveforCM

inside


void pre_auton

, it needs to be outside of any functions

so something like this:


void DriveforCM (double DistanceCM, int y) {
    
        //Define variables
        double wheelDiameterCM  = 10.16;
        double travelTargetCM = DistanceCM; //Wil determine how far the robot goes
    
        //Calc wheel circumferance
        double circumference = wheelDiameterCM * M_PI; 
        double degreesToRotate = (360 * travelTargetCM) / circumference; //All calculations are complete. Start the rest of the program.
    
        //Set the velocity of the left and right motor to 50% power. This command will not make the motor spin.
        LeftDrive.setVelocity(y, vex::velocityUnits::pct); 
        RightDrive.setVelocity(y, vex::velocityUnits::pct);
    
        //Rotate the Left and Right Motor for degreesToRotate. 
        LeftDrive.rotateFor(degreesToRotate, vex::rotationUnits::deg); //This command must be non blocking.
        RightDrive.rotateFor(degreesToRotate, vex::rotationUnits::deg); //This command is blocking so the program will wait here until the right motor is done.  
    
        //Stop motors after reached CM distance
        LeftDrive.stop();
        RightDrive.stop();
   }   

void pre_auton( void ) {
  // All activities that occur before the competition starts
  // Example: clearing encoders, setting servo positions, ...
   
    DriveforCM(50, 100);
}


#9

Oh so can a void not be inside of a void?


#10

A function of any type cannot have its definition inside any other function

you can still use functions inside other functions, you just cant define in them


#11

Ok thank you


#12

np, if you have any other questions feel free to ask


#13

@abennett5139 “void” and “int” in function declarations and definitions just refer to the data type of the value they return. (You can have ones that return double, *char, int], etc.) They are distinct from what the thing being defined is. Saying


int main () {}

is defining a function with a return value of type int, and your


void DriveforCM (double DistanceCM, int y) {}

is defining a function with a return value of type void (that is, it returns nothing).


#14

Just a question because I know what integers, chars, floats, etc are but what is a double


#15

double is basically 2 x the precision of a float.
eg. if a float variable is using 32 bits, then a double will be using 64 bits.


#16

double precision floating point number.