错误:控件已到达非无效函数的末尾

Error: control Reaches end of non void function(错误:控件已到达非无效函数的末尾)
本文介绍了错误:控件已到达非无效函数的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C++,我从课本上复制了这段代码,在编译代码的时候,最后出现了一个错误。错误显示:

控件到达非void函数末尾

,位于代码末尾:

#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"**

推荐答案

Welloperator<<定义为返回std::ostream&

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

但是您没有返回语句,这是undefined behavior,这意味着您不能依赖程序的行为,结果是不可预测的。看起来您应该:

return output ;

在函数的末尾。我们可以从C++标准草案6.6.3返回语句第2段中看到这是未定义的行为:

[.]从函数末尾流出等同于没有值的返回;这会导致值返回函数中的未定义行为。[.]

这篇关于错误:控件已到达非无效函数的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

C++ find method not working(C++查找方法不起作用)
How to determine the offset of an element of a tuple at compile time?(如何在编译时确定元组元素的偏移量?)
Error: Jump to case label in switch statement(错误:跳转到SWITCH语句中的CASE标签)
Order of elements in set of pointers(指针集中元素的顺序)
error while loading shared libraries: jvm.dll(加载共享库时出错:jvm.dll)
Why is quot;using namespace X;quot; not allowed at class/struct level?(为什么不允许在类/结构级别使用命名空间X;quot;?)