Hi,

I have to initiates a object to a variable in the private section of my class deceleration. The problem is that I can not add a try catch to it in this way. I Don't think I am explaining things to well (it's been a long day) so here is my current relevant code.

class Controller {
private:
	PlayerCc::PlayerClient robot ;// = newPlayerCc::PlayerClient("localhost");
	PlayerCc::RangerProxy rp;//(&robot,0);
	PlayerCc::Position2dProxy pp;
	Nunet net;
public:
	Controller(vector<float> cromo);
};

inline Controller::Controller(vector<float> cromo)
: robot("localhost"),
  rp(&robot,0),
  pp(&robot,0)

{
	//robot = new PlayerCc::PlayerClient("localhost");
	getSonar();
	net.init(16,8,2);
	net.setWeights(cromo);
}

The line I am concerned with is "PlayerCc::PlayerClient robot;" as I have to initialize it during the setup of the constructor (not sure of the technical term) such as ":robot("localhost")," but currently this throws out a error and i would like to catch it to see what it says. I can't see how to attach a try/catch block to that piece of code though.

I should tell you about the few things I have tried. i have tried changing the line "PlayerCc::PlayerClient robot;" to "PlayerCc::PlayerClient robot = new PlayerCc::PlayerClient("localhost");" with no luck. I have also tried setting "PlayerCc::PlayerClient robot = NULL;" but that was not allowed I have also tried not setting it at all but that also failed.

Sorry if this is incomprehensible. If you would like a rewrite when my brain is working just tell me what you don't understand.

Thanks

Alex

Recommended Answers

All 3 Replies

You can use a try/catch block in the initializer list. Check out this site. Here is the live code as an example.

#include <iostream>

struct BadObject{
 BadObject() {}
 BadObject(int i){ throw("Why you no work 0_o?"); }
};

struct GoodObject
{
 BadObject b;
 GoodObject(int i)
   try : b( BadObject(i) )
   {}
   catch(const char *p)
   { cout << "Runtime Error: " << p << endl; }
};

int main(){
 GoodObject obj(0);

 return 0;
}

IT BUILDS :) Thank you. Just what I needed as I was giving up hope for today.

IT BUILDS :) Thank you. Just what I needed as I was giving up hope for today.

No problem, glad to be able to help. Godspeed, take care

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.