문제

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"**
도움이 되었습니까?

해결책

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. [...]

다른 팁

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;

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;
} 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top