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";
			 }

Dani AI

Generated

The compiler warning "not all control paths return a value" means your operator[] has at least one path that reaches the end of the function without returning a double&. Printing an error message (e.g. with cerr) and falling off the end does not satisfy the requirement — you must either return a valid reference, throw, or terminate.

As noted, every path must return or otherwise end the function. As discovered, calling exit(1) removes the warning because it never returns — but terminating the whole program is usually too heavy-handed for a class library. Also avoid return 0.0 when the signature is double&: that would return a reference to a temporary and produce a dangling reference (undefined behavior), which is why compilers warn.

A cleaner, safer approach is to store coordinates in a small container and use its checked access, or explicitly throw on out-of-range. Example using std::array:

#include <array>
#include <stdexcept>

struct Point {
    std::array<double,3> v;
    double& operator[](std::size_t i)       { return v.at(i); } // throws std::out_of_range
    const double& operator[](std::size_t i) const { return v.at(i); }
};

Practical guidance:

  • Decide semantics up front: operator[] traditionally is unchecked (fast). Provide a separate checked at() that throws.
  • For debug-only checks use assert(i < 3) and return v[i] for speed in release builds.
  • For library code prefer throwing std::out_of_range to let callers handle errors; avoid exit() except for application-level fatal errors.
  • Use std::size_t for indices and provide both const and non-const overloads.
  • To silence the compiler you must ensure every path either returns or throws (or call a no-return function like std::terminate).

These choices remove the warning and make behavior explicit and safe.

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.