Error in my code building I can't seem to resolve

Howdy, I’m writing some code in the comp format and i’ve got a bug that I can’t seem to resolve. Ill attach the code below. I know the error is telling me that i’ve defined several variables in more than one place, but i’ve looked over my code several times and I simply do not see any instance of me defining the variables anywhere but under DriveFunctions.

This is the error message:

[info]: Saving Project …

[info]: Project saved!

windows build for platform vexv5

“LINK build/POWERHUBSKILLS.elf”

build/src/GeneralFunctions.o: In function `ScreenReadOut()':

GeneralFunctions.cpp:(.text._Z13ScreenReadOutv+0x0): multiple definition of `ScreenReadOut()’

build/src/main.o:main.cpp:(.text._Z13ScreenReadOutv+0x0): first defined here

build/src/DriveFunctions.o:(.bss.FB+0x0): multiple definition of `FB’

build/src/main.o:(.bss.FB+0x0): first defined here

build/src/DriveFunctions.o:(.bss.PowerH+0x0): multiple definition of `PowerH’

build/src/main.o:(.bss.PowerH+0x0): first defined here

build/src/DriveFunctions.o:(.bss.RL+0x0): multiple definition of `RL’

build/src/main.o:(.bss.RL+0x0): first defined here

build/src/DriveFunctions.o: In function `DriveTrain()':

DriveFunctions.cpp:(.text._Z10DriveTrainv+0x0): multiple definition of `DriveTrain()’

build/src/main.o:main.cpp:(.text._Z10DriveTrainv+0x0): first defined here

build/src/DriveFunctions.o: In function `catty()':

DriveFunctions.cpp:(.text._Z5cattyv+0x0): multiple definition of `catty()’

build/src/main.o:main.cpp:(.text._Z5cattyv+0x0): first defined here

build/src/DriveFunctions.o: In function `Intake()':

DriveFunctions.cpp:(.text._Z6Intakev+0x0): multiple definition of `Intake()’

build/src/main.o:main.cpp:(.text._Z6Intakev+0x0): first defined here

build/src/DriveFunctions.o:(.bss.fire+0x0): multiple definition of `fire’

build/src/main.o:(.bss.fire+0x0): first defined here

build/src/DriveFunctions.o:(.data.fire_speed+0x0): multiple definition of `fire_speed’

build/src/main.o:(.data.fire_speed+0x0): first defined here

build/src/DriveFunctions.o:(.bss.mode+0x0): multiple definition of `mode’

build/src/main.o:(.bss.mode+0x0): first defined here

build/src/DriveFunctions.o:(.bss.toggle_I+0x0): multiple definition of `toggle_I’

build/src/main.o:(.bss.toggle_I+0x0): first defined here

build/src/DriveFunctions.o:(.bss.toggle_S+0x0): multiple definition of `toggle_S’

build/src/main.o:(.bss.toggle_S+0x0): first defined here

build/src/DriveFunctions.o:(.bss.toggle_W+0x0): multiple definition of `toggle_W’

build/src/main.o:(.bss.toggle_W+0x0): first defined here

make: *** [vex/mkrules.mk:18: build/POWERHUBSKILLS.elf] Error 1

[error]: make process closed with exit code : 2

And this is my code:
(main.cpp)

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       VEX                                                       */
/*    Created:      Thu Sep 26 2019                                           */
/*    Description:  Competition Template                                      */
/*                                                                            */
/*----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// MotorPR              motor         1               
// MotorPL              motor         11              
// MotorBR              motor         2               
// MotorBL              motor         12              
// MotorTR              motor         3               
// MotorTL              motor         13              
// Controller1          controller                    
// OpticalS             optical       21              
// Inertial             inertial      20              
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "iostream"
#include "vex.h"
#include "DriveFunctions.cpp"
#include "GeneralFunctions.cpp"
#include "AutonFunctions.cpp"

using namespace std;
using namespace vex;
// A global instance of competition
competition Competition;

// define your global instances of motors and other devices here

/*---------------------------------------------------------------------------*/
/*                          Pre-Autonomous Functions                         */
/*                                                                           */
/*  You may want to perform some actions before the competition starts.      */
/*  Do them in the following function.  You must return from this function   */
/*  or the autonomous and usercontrol tasks will not be started.  This       */
/*  function is only called once after the V5 has been powered on and        */
/*  not every time that the robot is disabled.                               */
/*---------------------------------------------------------------------------*/

void pre_auton(void) {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // All activities that occur before the competition starts
  // Example: clearing encoders, setting servo positions, ...
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              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) {
    // This is the main execution loop for the user control program.
    // Each time through the loop your program should update motor + servo
    // values based on feedback from the joysticks.
  task Task3 = task(catty);
  task Task1 = task(ScreenReadOut);
  task Task2 = task(DriveTrain);
  task Task4 = task(Intake);
    // ........................................................................
    // Insert user code here. This is where you use the joystick values to
    // update your motors, etc.
    // ........................................................................

    wait(20, msec); // Sleep the task for a short amount of time to
                    // prevent wasted resources.
  }
}

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

  // Run the pre-autonomous function.
  pre_auton();

  // Prevent main from exiting with an infinite loop.
  while (true) {
    wait(100, msec);
  }
}

(DriveFunctions)

#include "vex.h"

int mode = 0;
int fire = 0;
int fire_speed=100;
int toggle_W =0;
int toggle_S =0;
int toggle_I =0;
int FB=0;
int RL=0;
motor_group PowerH= motor_group(MotorPR,MotorPL);

int DriveTrain(){

while(true){

    while(mode==0){
      int ABR = (abs(Controller1.Axis1.position())+abs(Controller1.Axis2.position()));
      int ABL= (abs(Controller1.Axis3.position())+abs(Controller1.Axis4.position()));
      int rev=0;
      if(ABL>ABR){
        FB = Controller1.Axis3.position();
        RL = Controller1.Axis4.position();
        rev=1;
      }
      else{
        FB = -(Controller1.Axis2.position());
        RL = -(Controller1.Axis1.position());
        rev=-1;
      }
      if((Controller1.Axis3.position())-(Controller1.Axis4.position())<1&&(Controller1.Axis3.position())+(Controller1.Axis4.position())<1||
        (Controller1.Axis3.position())-(Controller1.Axis4.position())>1&&(Controller1.Axis3.position())+(Controller1.Axis4.position())>1){
        MotorBR.spin(fwd,FB-(RL*rev),pct);
        MotorTR.spin(fwd,FB-(RL*rev),pct);
        MotorBL.spin(fwd,FB+(RL*rev),pct);
        MotorTL.spin(fwd,FB+(RL*rev),pct);
        }
      else{
        MotorBR.spin(fwd,(FB-(RL*rev))/1.7,pct);
        MotorTR.spin(fwd,(FB-(RL*rev))/1.7,pct);
        MotorBL.spin(fwd,(FB+(RL*rev))/1.7,pct);
        MotorTL.spin(fwd,(FB+(RL*rev))/1.7,pct);
      }
      //checks to see if you are trying to change mode.

      if (Controller1.ButtonA.pressing()) {
          mode = 1;
          wait(300,msec);
      }     
    }
    while(mode==1){
      //tank
      MotorTR.spin(fwd,(Controller1.Axis2.position()),pct);
      MotorBR.spin(fwd,(Controller1.Axis2.position()),pct);
      MotorTL.spin(fwd,(Controller1.Axis3.position()),pct);
      MotorBL.spin(fwd,(Controller1.Axis3.position()),pct);
      //checks to see if you are trying to change mode
      if (Controller1.ButtonA.pressing()) {
          mode = 0;
          wait(300,msec);
      }
    }
}
return 0;
}

//Task for cattipult that will be run repeatedly durring driver controll
int catty() {
while(true){
  if(Controller1.ButtonR2.pressing()){
    if(toggle_S ==1){
      toggle_S=0;
      PowerH.stop();
      wait(300,msec);
    }
    else if(toggle_S==0){
      toggle_S=1;
      PowerH.spin(fwd,fire_speed,pct);
      wait(300,msec);
    }
  }

  if(Controller1.ButtonUp.pressing()){
    fire_speed= fire_speed-10;
  }
  if(Controller1.ButtonDown.pressing()){
    fire_speed= fire_speed+10;
  }
  wait(250,msec);
}
return 0;
}

//Intake code for driver controll
int Intake(){
  while(true){
    while(Controller1.ButtonL1.pressing()){
      PowerH.spin(fwd,100,pct);
      wait(300,msec);
    }
    while(Controller1.ButtonL2.pressing()){
      PowerH.spin(reverse,100,pct);
      wait(300,msec);
    }
    PowerH.stop();
    wait(300,msec);
  }
return 0;
}

Please help!

1 Like

Don’t include c++ files in main.cpp. The compiler is essentially compiling them twice, once as an included file and again when it finds them (presumably) in the src folder.

There’s an example (sort of) of how to structure the project here.

4 Likes