[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.

#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);


The problem I’m having is with my JoystickOne class. To avoid the hassle and inefficiency of creating an array of Victors in it (and other classes), I thought I would just create an array of pointers to Victors in MyRobot.cpp and pass it by means of an argument in the constructor. Where I’m really running into issues is the RobotDemo block, re-posted below

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

It seems like the code between the “:” and the “{” can only be used for calling constructors, and the code within the curly brackets is used for actually assigning values to variables. That’s my (basic) understanding.

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)

#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() {
}


First of all, you can make

for (int x = 0; x < 10; x++) {
		buttons[x] = false;
	}

into this:


buttons = {0}; //This fills the array with 0's, which is equivalent to false.

Secondly, I am partial against using the “<=” operator in a for loop; I suggest just sticking with “<”.

*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:


int foo[10] = {0};
for(int i = 0; i < 10; i++)
  foo* = i;
//Is the same as this:
for(int i = 0; i < 10; i++)
  *(foo+i) = i;

So, that being said, look at how you declared your Victor array. I don’t think you are doing what you are trying to do.

I would do:


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);


Hopefully that helps. I might have made a simple mistake here or there, but keep in mind that I’ve been almost exclusively been programming in C this year.*