i write a point class, when i compile , vs2008 give me the warning about" not all control parth return a value" could you please tell how to avoid that?

double& operator [](int i)    //[] which can used as the left value
			{  
			    if(i==0) return x;
				else if(i==1) return y;
				else if(i==2) return z;
				else cerr<<"our of rand index of Point";
			 }

Recommended Answers

All 2 Replies

double& operator [](int i)    //[] which can used as the left value
			{  
			    if(i==0) return x;
				else if(i==1) return y;
				else if(i==2) return z;
				else cerr<<"our of rand index of Point";
			 }

The function has a return type of double which you have returned three times in your above code. The error is due to a lack of a 4th return from your if-else statement. You need to return or exit the function; or possibly some other solution that I can't think of at the moment.

else cerr<<"our of rand index of Point";
// either return here or exit() prior to the end of block of code

i.e.

double& operator [](int i)    //[] which can used as the left value
			{  
			    if(i==0) return x;
				else if(i==1) return y;
				else if(i==2) return z;
				else{ 
                                   cerr <<"our of rand index of Point";
                                   // return 0.0 or
                                   // exit(1)
                                   // some sort of error handling
			        }
                         }
double& operator [](int i)    //[] which can used as the left value
			{  
			    if(i==0) return x;
				else if(i==1) return y;
				else if(i==2) return z;
				else cerr<<"our of rand index of Point";
			 }

The function has a return type of double which you have returned three times in your above code. The error is due to a lack of a 4th return from your if-else statement. You need to return or exit the function; or possibly some other solution that I can't think of at the moment.

else cerr<<"our of rand index of Point";
// either return here or exit() prior to the end of block of code

i.e.

double& operator [](int i)    //[] which can used as the left value
			{  
			    if(i==0) return x;
				else if(i==1) return y;
				else if(i==2) return z;
				else{ 
                                   cerr <<"our of rand index of Point";
                                   // return 0.0 or
                                   // exit(1)
                                   // some sort of error handling
			        }
                         }

Thank you very much for your information. the 'exit(1)' works very well.
but "return 0.0 " will gives a warning about" return a temperary variable".
Regards.

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.