Globally declaring an object with arguments

Hi Guys,

I have a class voInterpreter.cpp and it has a constructor


voInterpreterClient::voInterpreterClient(Connection_Info connIn, int maxInputBufferSize) {
_connInfo = connIn;

if(maxInputBufferSize > 0) {
_maxInputBufferSize = maxInputBufferSize;
_buffer = new char[maxInputBufferSize];
}

};


Now, I need to create an object to this class by calling the constructor from another class Robot.cpp. I can do that from a function in Robot.cpp but I cannot access it from another functions of Robot.cpp. I need to create an object say client and access it in all the functions of Robot.cpp.

Could anyone please help me in creating an object globally. The code below is Robot.h and I created static voInterpreter client which doesn't give me what I need.



#ifndef Robot_H
#define Robot_H

#include <device/robot.h>

#include "voInterpreterClient.h"

// number of DOFs of the robot
#define MAX_SERVOS 63

#define SIMULATION_STEP_DURATION 16

#define STEP_NBR 180

#define STR_BUFFER_SIZE 512

class Robot {
public:
// constructor: create robot according to specification
Robot();

// destructor
virtual ~Robot() {};

void enableServoPosition(int servoId);
void setServoPosition(int servoId, float value);
float getServoPosition(int servoId);

void save_pose();
void load_pose(int id);
void create_pose();

void run();
void wait(float x);
//void play();

// Edited by Sridhar
void setupConnection();
void transmitData();
void servo_positive(int,double);
void servo_negative(int,double);
void save_image();
void process_image();

void find_ball();

void turn_right_90();
void turn_left_90();
void turn_clockwise_180();
void backward_linear_motion();
void forward_linear_motion();
void move_forward_turn_right();
void move_forward_turn_left();
void square_motion();
// Edited by Sridhar

static const char * SERVO_NAMES[MAX_SERVOS+1];
static const float SERVO_LIMITS[MAX_SERVOS][2];

private:
float _mouthPos;
float _controlStep; // simulation step size in milliseconds
DeviceTag servos[MAX_SERVOS];

int pose_file_id, save_file_id, current_servo;
int counter,key;

const unsigned char *image;

//protected:
// static voInterpreterClient client;

};

#endif


Thank you for your assistance
Well you commented out the static variable declaration. That works if all Robots use the same instance of voInterpreterClient. (In which case you just need to instantiate the variable in Robot.cpp).

Or if all Robots have a different instance then remove the keyword "static" from the declaration of client.

[BTW: your voInterpreterClient constructor is evil. Consider that _maxInputBufferSize and _buffer are initialized only if maxInputBufferSize is >0.]
Hi Jsmith,

First of all thank you for your advice.

So, can I use static and declare the object. Moreover, I should initialize it in another function so can I do this

voInterperterClient client(ConnectionInfo,maxInputBufferSize);


Can I access client object in any function and call functions of voInterpreterClient.cpp ?

The voInterpreterClient code was not written by me. Anyways, thank you so much for letting me know. I would make the necessary changes.


Topic archived. No new replies allowed.