|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
[Q] I need help with passing an array of pointers to an object
I'm trying to code our team's bot using C++, and so far I've been working off of WPI's SimpleTemplate. I'm having some issues with it though.
Here's the code of the MyRobot.cpp, the main file. Code:
#include "WPILib.h"
#include "JoystickOne.h"
/**
* This is a demo program showing the use of the RobotBase class.
* The SimpleRobot class is the base of a robot application that will automatically call your
* Autonomous and OperatorControl methods at the right time as controlled by the switches on
* the driver station or the field controls.
*/
class RobotDemo : public SimpleRobot
{
Joystick stick;
JoystickOne joyOne;
RobotDrive myRobot; // robot drive system // only joystick
Victor *victors [8];
/*Restore To*/
public:
RobotDemo(void):
stick(1),
joyOne(&stick),//
myRobot(1, 2)// these must be initialized in the same order
// as they are declared above.
{
for (int x = 1; x <= 7; x++) {
victors[x] = new Victor(x);
}
myRobot.SetExpiration(0.1);
}
/**
* Drive left & right motors for 2 seconds then stop
*/
void Autonomous(void)
{
myRobot.SetSafetyEnabled(false);
myRobot.Drive(-0.3, 0.0); // drive forwards half speed
Wait(2.0); // for 2 seconds
myRobot.Drive(0.0, 0.0); // stop robot
}
/**
* Runs the motors with arcade steering.
*/
void OperatorControl(void)
{
myRobot.SetSafetyEnabled(true);
while (IsOperatorControl())
{
myRobot.ArcadeDrive(stick);// drive with arcade style (use right stick)
joyOne.testForActions(); /*Check joystick one for actions*/
Wait(0.005); // wait for a motor update time
}
}
/**
* Runs during test mode
*/
void Test() {
}
};
START_ROBOT_CLASS(RobotDemo);
Code:
RobotDemo(void):
stick(1),
joyOne(&stick),//
myRobot(1, 2)// these must be initialized in the same order
// as they are declared above.
{
for (int x = 1; x <= 7; x++) {
victors[x] = new Victor(x);
}
myRobot.SetExpiration(0.1);
}
Anyways, I want to pass an array of pointers to victors to the joyOne() costructor, but it doesnt look like I can actually populate the array until after I pass it. To work around this I think I need to pass a pointer to the array of pointers and then, whenever I need to use it in a method, take the pointer to the pointer array and perform methods on the victor from there; this is where my head starts to explode. I'm new to C++ and pointers, so the syntax and code required to do this is defeating me. If you guys could help that'd be great. I know this might be a confusing explanation, but the truth is I'm confused myself. Incidentally, here's my JoystickInput class (joystickone extends it) Code:
#ifndef JOYSTICKINPUT_H //the .h
#define JOYSTICKINPUT_H
#include "WPILib.h"
class JoystickInput {
public:
JoystickInput(Joystick*);
JoystickInput(Joystick*, Victor**); //This line is supposed to be the constructor taking the
//pointer to the Victor pointer array, but it doesnt work
Joystick * joystick;
bool buttons [10];
Victor** victors [8];
bool buttonClicked(int id);
virtual void testForActions();
};
#endif
//Now for the .cpp
#include "JoystickInput.h"
JoystickInput::JoystickInput(Joystick * joy) {
joystick = joy;
for (int x = 0; x < 10; x++) {
buttons[x] = false;
}
//constructor that works
}
JoystickInput::JoystickInput(Joystick * joy, Victor** vicArray) {
joystick = joy;
for (int x = 0; x < 10; x++) {
buttons[x] = false;
}
for (int n = 0; n <=7; n++) {
*victors = vicArray;
}
//doesnt work. syntax to make it do so?
}
bool JoystickInput::buttonClicked(int id) {
//not relevant
}
void JoystickInput::testForActions() {
}
Last edited by pipsqueaker : 01-05-2013 at 18:36. Reason: Clarity, more relevant code |
|
#2
|
|||
|
|||
|
Re: [Q] I need help with passing an array of pointers to an object
First of all, you can make
Code:
for (int x = 0; x < 10; x++) {
buttons[x] = false;
}
Code:
buttons = {0}; //This fills the array with 0's, which is equivalent to false.
*victor points to the pointer to a pointer to an array of pointers to Victor objects. Now, what many C/C++ novices overlook is the fact that the array and pointer notations are interchangeable. So, let's see this example: Code:
int foo[10] = {0};
for(int i = 0; i < 10; i++)
foo[i] = i;
//Is the same as this:
for(int i = 0; i < 10; i++)
*(foo+i) = i;
I would do: Code:
Victor **victors;
//All the other stuff...
JoystickInput::JoystickInput(Joystick * joy, Victor **vicArray) {
joystick = joy;
buttons = {0};
victors = vicArray;
}
//Then when you declare that array that you pass in:
Victor *drivers = new Victor[8];
//Fill the array like drivers[0] = new Victor();
//Then pass it into the function.
JoystickInput(joy, &drivers);
Last edited by davidthefat : 01-05-2013 at 21:33. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|