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++ 中计算滚动/移动平均值 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01