4

I am learning C++ and I copied this code from a textbook, while compiling the code, an error appears at the end. The error says:

Control Reaches end of non-void function

and its located at the end of the code:

#include "ComplexNumber.hpp"
#include <cmath>

ComplexNumber::ComplexNumber()
{
mRealPart = 0.0;
mImaginaryPart = 0.0;
}

ComplexNumber::ComplexNumber(double x, double y)
{
mRealPart = x;
mImaginaryPart = y;
}

double ComplexNumber::CalculateModulus() const
{
return sqrt(mRealPart*mRealPart+
            mImaginaryPart*mImaginaryPart);
}
double ComplexNumber::CalculateArgument() const
{
return atan2(mImaginaryPart, mRealPart);
}

ComplexNumber ComplexNumber::CalculatePower(double n) const
{
double modulus = CalculateModulus();
double argument = CalculateArgument();
double mod_of_result = pow(modulus, n);
double arg_of_result = argument*n;
double real_part = mod_of_result*cos(arg_of_result);
double imag_part = mod_of_result*sin(arg_of_result);
ComplexNumber z(real_part, imag_part);
return z;
}

ComplexNumber& ComplexNumber::operator=(const ComplexNumber& z)
{
mRealPart = z.mRealPart;
mImaginaryPart = z.mImaginaryPart;
return *this;
}

ComplexNumber ComplexNumber::operator-() const
{
ComplexNumber w;
w.mRealPart = -mRealPart;
w.mImaginaryPart = -mImaginaryPart;
return w;
}

ComplexNumber ComplexNumber::operator+(const ComplexNumber& z) const
{
ComplexNumber w;
w.mRealPart = mRealPart + z.mRealPart;
w.mImaginaryPart = mImaginaryPart + z.mImaginaryPart;
return w;
}

std::ostream& operator<<(std::ostream& output,
                     const ComplexNumber& z)
{
output << "(" << z.mRealPart << " ";
if (z.mImaginaryPart >= 0.0)
{
    output << " + " << z.mImaginaryPart << "i)";
}
else
{
    output << "- " << -z.mImaginaryPart << "i)";
}
} //-------->>>>**"Control Reaches end of non-void function"**
toasted_flakes
  • 2,502
  • 1
  • 23
  • 38
heri-salmas
  • 103
  • 2
  • 12
  • Whatever textbook this is from, I don't imagine it's all that good. – chris Feb 24 '14 at 03:13
  • The book is by Joe Pitt-Francis and Jonathan Whiteley. "Guide to Scientific Computing in C++". I am reading it, because I am supposed to do some numerical calculations using object oriented approach and is the only one I could find, If you have any suggestion, i will be glad to hear them. – heri-salmas Feb 24 '14 at 03:16
  • To be quite honest, scientists and *good* programming don't go together very well. You'll end up getting a lot more out of those books that aren't designed for teaching the language if you know it well enough to not pick up bad habits. For the language, we have a nice list of [books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – chris Feb 24 '14 at 03:18
  • Thanks Chris!, I am a scientist and I am more used to work with Java and Fortran, now I have to work in a project where we are using C++. And you are right. Even without understanding C++ I can spot many bad programing habits. I will check the list of books suggested. And try to improve the image you have of scientist in the programming world! – heri-salmas Feb 24 '14 at 03:23

3 Answers3

1

Well operator<< is defined to return std::ostream&:

std::ostream& operator<<(std::ostream& output, const ComplexNumber& z)
^^^^^^^^^^^^^

but you have no return statements, this is undefined behavior and means you can not rely on the behavior of the program, the results are unpredictable. It looks like you should have:

return output ;

at the end of the function. We can see this is undefined behavior from the draft C++ standard section 6.6.3 The return statement paragraph 2 which says:

[...] Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function. [...]

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
1

The function claims to return something:

std::ostream& operator<<(std::ostream& output, const ComplexNumber& z)
^^^^^^^^^^^^^

but there is no return statement at the end. You should add one:

return output;
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

This function

std::ostream& operator<<(std::ostream& output,
                     const ComplexNumber& z)
{
output << "(" << z.mRealPart << " ";
if (z.mImaginaryPart >= 0.0)
{
    output << " + " << z.mImaginaryPart << "i)";
}
else
{
    output << "- " << -z.mImaginaryPart << "i)";
}
} 

has return type std::ostream & However it returns nothing. I think there is a typo There should be

std::ostream& operator<<(std::ostream& output,
                     const ComplexNumber& z)
{
output << "(" << z.mRealPart << " ";
if (z.mImaginaryPart >= 0.0)
{
    output << " + " << z.mImaginaryPart << "i)";
}
else
{
    output << "- " << -z.mImaginaryPart << "i)";
}

return output;
} 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335