Log in

View Full Version : Pointers Issues


cjlane1138
20-01-2012, 00:29
Hey everyone,

So, we are having issues with pointers. I just wanted to know if anyone else is getting the same errors. They are kernel task exceptions. When I get rid of the pointers and initialize the classes another way, it all works.

If anyone has a solution, please tell me. Otherwise, I just want to know if everyone else is having the same issue or it's just me....


#include "WPILib.h"
#include "Drive.h"

class RobotDemo : public SimpleRobot
{
Drive *dr;

public:
RobotDemo(void)
{
GetWatchdog().Kill();
dr = new Drive();
//AxisCamera &camera = AxisCamera::GetInstance();
dr->myRobot->SetExpiration(0.1);
}

/**
* Drive left & right motors for 2 seconds then stop
*/
void Autonomous(void)
{
GetWatchdog().Kill();
//dr->myRobot->SetSafetyEnabled(false);
dr->myRobot->Drive(0.5, 0.0); // drive forwards half speed
Wait(2.0); // for 2 seconds
dr->myRobot->Drive(0.0, 0.0); // stop robot
}



void OperatorControl(void)
{
GetWatchdog().Kill();
//dr->myRobot->SetSafetyEnabled(true);
while (IsOperatorControl())
{
printf("In Operator\n");
dr->myRobot->TankDrive(dr->leftstick, dr->rightstick);
Wait(0.01);
}
}

void Disabled(void)
{
printf("Disabled\n");
}
};

START_ROBOT_CLASS(RobotDemo);




It keeps giving me that the Drive *dr; has not been initialized correctly, where I believe I have. Anyone know any ways I can debug this?

Thanks,
-Eagle Engineering 1138

mikets
20-01-2012, 00:48
If you suspect you have a problem with your code, you need to post it so other people can help you.

cjlane1138
20-01-2012, 11:42
If you suspect you have a problem with your code, you need to post it so other people can help you.
Just posted the code in the main post.

Alan Anderson
20-01-2012, 12:16
dr = new Drive();



It keeps giving me that the Drive *dr; has not been initialized correctly, where I believe I have. Anyone know any ways I can debug this?


Is there indeed a Drive() constructor with no parameters?

cjlane1138
20-01-2012, 12:38
Is there indeed a Drive() constructor with no parameters?
No, I have a separate Drive class.

Drive.h


#ifndef DRIVE_H_
#define DRIVE_H_
#include "WPILib.h"

class Drive
{
public:
Drive();

RobotDrive *myRobot;

Jaguar *leftmotor;
Jaguar *rightmotor;

Joystick *leftstick;
Joystick *rightstick;
};

#endif


Drive.cpp


#include "WPILib.h"
#include "Drive.h"

Drive::Drive()
{
printf("Initializing Drive.cpp\n");

leftmotor = new Jaguar(1);
rightmotor = new Jaguar(2);

myRobot = new RobotDrive(leftmotor, rightmotor);

leftstick = new Joystick(1);
rightstick = new Joystick(2);
};


There is the header file and the cpp file.
MyRobot is in the main post.

wireties
20-01-2012, 19:11
Hey everyone,

So, we are having issues with pointers. I just wanted to know if anyone else is getting the same errors. They are kernel task exceptions. When I get rid of the pointers and initialize the classes another way, it all works.

Another way? What way works? Do you know which line kills it?

Alan Anderson
20-01-2012, 22:33
I seem to remember something about needing to initialize things in the same order they were defined. This is just a guess, but have you tried moving the
myRobot = new RobotDrive(leftmotor, rightmotor); line to the very beginning of Drive::Drive()?