I have a class named Robot.cpp and another voInterpreterClient.cpp. I need to create instance of voInterpreterClient.cpp in Robot.cpp globally. voInterpreterClient.cpp contains a constructor
What do you mean by "globally"? Do you mean one instance for every Robot object? If that's the case you can use a static voInterpreterClient in your Robot class:
I am creating instance of voInterpreterClient in void Robot::setupConnection() as
voInterpreterClient client(conn, 1024);
Everything, is fine till here. But, I need to access the client object in void Robot::transmitData(). For that I need to declare voInterpreterClient client object as a global one. Here, are the functions
void Robot::setupConnection()
{
//Declare the connection object
Connection_Info conn;
// Reading IP address and port no. from a configuration file
ifstream _config;
string _ipaddr,_hostInfo[2];
_config.open("configuration.txt");
int i=0;
while(getline(_config,_ipaddr,':'))
{
_hostInfo[i]=_ipaddr;
i++;
}
ofstream _configLog;
_configLog.open("configuration_log.txt");
_configLog << "// This is the IP address and port number used by the Humanoid robot to connect to Inpterpreter server" << "\n" << endl;
_configLog << "IP Address : " << conn.host << endl << "Port : " << conn.port << endl;
// Instantiate the new Client:
voInterpreterClient client(conn, 1024);
if(!client.voConnect())
{
robot_console_printf("Could not establish connection with the server\n");
exit(0);
}
else
{
robot_console_printf("Connection established\n");
}
}
void Robot::transmitData()
{
int count=0;
string srvInput="",srvOutput="";
ostringstream _output;
// for non-blocking polling:
// Receiving input from the server
if(Robot::client.poll())
{
srvInput = Robot::client.getString();
if(srvInput=="q")
{
client.voDisconnect();
robot_console_printf("Connection terminated\n");
exit(0);
}
count++;
}
key=(int)srvInput[0];
robot_console_printf("ASCII value = %d\n",key);
// Posting reply to the server
if(!srvInput.empty() && count==1)
{
_output << key;
srvOutput=_output.str();
Robot::client.publish(srvOutput);
_output.str("");
count--;
}
}
Please, help me. When I did what you told me I get errors saying that
Robot.o: In function `Robot::transmitData()':
Robot.cpp:(.text+0x5d2): undefined reference to `Robot::client'
Robot.cpp:(.text+0x5ea): undefined reference to `Robot::client'
Robot.cpp:(.text+0x666): undefined reference to `Robot::client'
Robot.cpp:(.text+0x7e1): undefined reference to `Robot::client'
Put your code in code tags ( the # button to the right of the textbox when you're writing your post)
Can you also post your headers for Robot and voInterpreterClient?
ofstream _configLog;
_configLog.open("configuration_log.txt");
_configLog << "// This is the IP address and port number used by the Humanoid robot to connect to Inpterpreter server" << "\n" << endl;
_configLog << "IP Address : " << conn.host << endl << "Port : " << conn.port << endl;
// Instantiate the new Client:
voInterpreterClient client(conn, 1024);
if(!client.voConnect())
{
robot_console_printf("Could not establish connection with the server\n");
exit(0);
}
else
{
robot_console_printf("Connection established\n");
}
}
void Robot::transmitData()
{
int count=0;
string srvInput="",srvOutput="";
ostringstream _output;
// for non-blocking polling:
// Receiving input from the server
if(Robot::client.poll())
{
srvInput = Robot::client.getString();
if(srvInput=="q")
{
client.voDisconnect();
robot_console_printf("Connection terminated\n");
exit(0);
}
count++;
}
key=(int)srvInput[0];
robot_console_printf("ASCII value = %d\n",key);
// Posting reply to the server
if(!srvInput.empty() && count==1)
{
_output << key;
srvOutput=_output.str();
Robot::client.publish(srvOutput);
_output.str("");
count--;
}
}
Please do as JRaskell has asked and surround the posted code with code tags using the '#' button. You'll see this button to the right of your editor window.
void Robot::setupConnection()
{
// This is not a global instance. It is an instance created on the stack and will
// be destroyed the moment that setupConnection returns.
// Instantiate the new Client:
voInterpreterClient client(conn, 1024);
}
I see that you created the static instance within the header of the robot class. However, you did not initialize it in the .cpp file so you will get errors. Also if you create a static instance within the class, the object isn't global. It only exists for each Robot instance. Lastly, get rid of the instance that you are creating in the setupConnection function.
In Robot.cpp you need
voInterpreterClient Robot::client;
Moreover, you will need to add a default constructor to be able to construct the static instance of voInterpreterClient. The current constructor requires the connection info which isn't available at compile time. You'll need a separate member function to pass in the connection info later on.
Will there be many Robot instances? Should each robot have its own instance of voInterpreterClient? If so, then the static instance you have won't work anyway. There aren't enough details on your problem for me to help. I can only give you some possibilities and hints. If you only need the robot member functions to each access the same instance you could just have a non-static member of type voInterpreterClient so that each Robot has its own.