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
}
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.
}
}
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);
}
@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).