Referencing an object in another class

So, this is actually for my Vex V5 c++ programme, but the vex forums keep denying my account for some reason so I have come here instead.
I am trying to create a new class DriveTrain. Inside this class, I want to declare 4 of its variables as objects from another class. Elaborated, I want to be able to make a new object, dt, which stores all 4 of my motors in it. The motors are objects of another class.
Example:
(this is the .h config file)
1
2
3
4
vex::motor FLMotor (vex::PORT9, vex::gearSetting::ratio18_1,true);
vex::motor FRMotor (vex::PORT10, vex::gearSetting::ratio18_1,false);
vex::motor BLMotor (vex::PORT19, vex::gearSetting::ratio18_1,true);
vex::motor BRMotor (vex::PORT20, vex::gearSetting::ratio18_1,false);

----------------------------------------------------------------------
(this is the cpp file)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class DriveTrain{
   public:
   int motor1, motor2, motor3, motor4, dir, vel;
   dt((motor1, motor2, motor3, motor4);
   void drive(dir, vel){
   motor1.spin(directionType:: dir, vel, VelocityUnits::pct);
   motor2.spin(directionType:: dir, vel, VelocityUnits::pct);
   motor3.spin(directionType:: dir, vel, VelocityUnits::pct);
   motor4.spin(directionType:: dir, vel, VelocityUnits::pct);
   }
}
int main(){
   DriveTrain dt(FLMotor, FRMotor, BLMotor,BRMotor);
   dt.drive(fwd, 20);
}

I must admit, I'm not even sure if the syntax of the class is correct. If this isn't clear enough, what I'm aiming for is such:
The Vex class has object motors. I want to make a new DriveTrain class that allows me to assign specific motors to the new object. The Object function will then use those in its function, ultimately reducing the amount of typing I have to do (and pretty up my code). Is this even possible?
P.S. I couldn't quite figure out how the code format works since I couldn't see any actual changes, so I just typed it out with normal format.
Figured the formatting out at least.
Last edited on
So if I understand what you said, you have two classes, your DriveTrain class and another which you didn't include, let's say Motor class.

So you want to pass as an argument, an object of the motor class to the constructor of the drivetrain class.

Assuming, line 3, int motor1, motor2, motor3, motor4, dir, vel; that the motors there are those you wish to assign to objects from your motor class. In which case they shouldn't be ints, but Motor. Also if you haven't done it you should include your Motor class header file to your drivetrain file.

I don't know what your debugger is telling you, but what is dt((motor1, motor2, motor3, motor4); dt? it doesn't have a type in front of it. Is it a void function? Also you have one too many parenthesis.

Also I'm not sure this syntax is allowed : DriveTrain dt(FLMotor, FRMotor, BLMotor,BRMotor); since it's your constructor, I assume what you want to do is that:
1
2
3
4
DriveTrain (Motor FLMotor,Motor FRMotor,Motor BLMotor,Motor BRMotor)
{
    dt (FLMotor, FRMotor, BLMotor, BRMotor);
}


To use dt that way : dt.drive(fwd, 20); it has to be an object, but it looks like you defined it as a function above.

Last edited on
If I understand correctly, Motor is an object apart of the vex class (which is predefined by my coding studio, Vex Coding Studio). For example, you can say vex::motor.spin(arguments), where motor is the object of the vex class and spin is a function linked to motor. Of course, in the config file, vex::motor can be specified so that you can declare more than one. vex::motor "name of motor" ("port of motor").

What I want is a new object called dt on which I can use functions. Similar to how I can tell the motor to spin, I'd like to be able to do something like vex::dt.turnLeft("arguments"). The Drive train object would have to be able to call on the motor objects. I know I can do this with functions as such:
1
2
3
4
 void Drive(int dist, int pwr){ //dis=distance in revolutions, pwr= power
FLmotor.rotateTo(dis, rotationUnits::rev, pwr, veloctiyType::pct); //this is the command to spin to a specific point
//repeat for the other 3 motors, then create functions for turning left and turning right
}


I was told that if I was going to have multiple functions that affect the same set of "things" that it would simply be better to make a class, then make an object incorporating all those things, then simply define the functions inside the class.

Like I implied before, I've never really worked with classes so if I'm misinterpreting something, feel free to correct me (I beg you, please XD).
Topic archived. No new replies allowed.