Go to Post I never said there was anything wrong with the kit frame, simply that it was weak. - Viper37 [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

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #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: 51
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
 


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 06:09.

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