Go to Post I am a 2-3 year competition veteran, and a new member at chief delphi, and everyone here seems to be as nice as they are at the competition. Very gracious, you might say. - Ian S. [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 01-05-2013, 18:20
pipsqueaker pipsqueaker is offline
Registered User
FRC #1124
 
Join Date: Apr 2013
Location: Avon
Posts: 59
pipsqueaker is a name known to allpipsqueaker is a name known to allpipsqueaker is a name known to allpipsqueaker is a name known to allpipsqueaker is a name known to allpipsqueaker is a name known to all
[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);
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

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);
	}
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)
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
Reply With Quote
  #2   Spotlight this post!  
Unread 01-05-2013, 21:20
davidthefat davidthefat is offline
Alumni
AKA: David Yoon
FRC #0589 (Falkons)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2010
Location: California
Posts: 792
davidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud of
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;
	}
into this:

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

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

Last edited by davidthefat : 01-05-2013 at 21:33.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 18:38.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi