C++中的一元否定
Unary Negation in C++
我几乎完成了这个程序,但在应用一元否定函数时遇到了问题。
我制作了一个基于计算复数的程序,到目前为止,它运行得很好,除了整个负数部分。我似乎不知道如何让我的一元否定在代码中工作,而不让它警告我,并用它的运行时错误崩溃。
在我的h文件中,我有这个:
Complex operator-(const Complex &lhs);
在我的cpp文件中,我试图从上面写的内容创建这个:
Complex operator-(const Complex &lhs)
{
return Complex(-lhs);
}
我在cpp文件上写的代码给了我一个警告C4717错误,上面写着:"operator-':在所有控制路径上递归,函数将导致运行时堆栈溢出。"当然,应用上面的数字会导致运行时错误。
我相信你们中的一些人想要完整的代码,所以这里是:header、cpp和测试文件。:)
我很感激你的帮助。
头文件:
#ifndef com_H
#define com_H
#include <iostream>
#include <string>
using namespace std;
/********** CLASS **********/
class Complex
{
//The BFFs: Stream Insertion and Operators//
friend istream &operator >> (istream &lhs, Complex &rhs);
friend ostream &operator << (ostream &lhs, const Complex &rhs);
private:
float real, img;
public:
//Complex Constructor//
Complex(float new_real = 0.0, float new_img = 0.0);
//Complex Set-and-Get Functions//
void setComplex(float new_real, float new_img);
float getReal() const { return real; }
float getImg() const { return img; }
//Complex Functions//
Complex &operator+=(const Complex &rhs);
Complex &operator-=(const Complex &rhs);
Complex &operator*=(const Complex &rhs);
Complex &operator/=(const Complex &rhs);
};
/********** O P E R A T O R **********/
Complex operator+(const Complex &lhs, const Complex &rhs);
Complex operator-(const Complex &lhs, const Complex &rhs);
Complex operator*(const Complex &lhs, const Complex &rhs);
Complex operator/(const Complex &lhs, const Complex &rhs);
//COMPARISON OPERATORS*/
bool operator==(const Complex &lhs, const Complex &rhs);
bool operator!=(const Complex &lhs, const Complex &rhs);
bool operator<(const Complex &lhs, const Complex &rhs);
bool operator<=(const Complex &lhs, const Complex &rhs);
bool operator>(const Complex &lhs, const Complex &rhs);
bool operator>=(const Complex &lhs, const Complex &rhs);
//NEGATION//
Complex operator-(const Complex &lhs);
#endif
cpp文件:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <math.h>
#include "com.h"
using namespace std;
//
//***************** COMPLEX CONSTRUCTORS *****************//
//
Complex::Complex(float new_real, float new_img)
{
real = new_real;
img = new_img;
}
void Complex::setComplex(float new_real, float new_img)
{
real = new_real;
img = new_img;
}
//
//***************IF REAL NUMBER IS ZERO CHECK***************//
//
void checkNum(char ops, float new_img)
{
char i = 'i';
if (new_img == 0)
{
cout << "0";
}
else if (new_img != 1 && ops == '-')
{
new_img *= -1;
cout << new_img << i;
}
else if (new_img == 1 && ops == '-')
{
cout << "-i";
}
else if (new_img == 1)
{
cout << i;
}
else
{
cout << new_img << i;
}
}
//
//*****************STREAM INSERTIONS*****************//
//
istream &operator >> (istream &lhs, Complex &rhs)
{
char ops;
char i = 'i';
lhs >> rhs.real >> ops >> rhs.img >> i; //lhs is another name for cin
if (ops == '-')
{
rhs.img *= -1;
}
return lhs;
}
ostream &operator << (ostream &lhs, const Complex &rhs)
{
char ops;
char i = 'i';
float new_real = rhs.real;
float new_img = rhs.img;
if (new_img < 0)
{
ops = '-';
new_img *= -1;
}
else if (new_img >= 0)
{
ops = '+';
}
if (new_real == 0)
{
checkNum(ops, new_img);
return lhs << endl;
}
else if (new_img == 0)
{
return lhs << new_real;
}
else if (new_img == 1)
{
return lhs << new_real << " " << ops << " " << i;
}
else
{
return lhs << new_real << " " << ops << " " << new_img << i;
}
// lhs << rhs.real << " + " << rhs.img << 'i';
}
//
//***************COMPLEX ARITHMETIC OPERATORS***************//
//***************** ( + | - | * | / |) ***************** //
//
// FORMULA for COMPLEX NUMBERS :: (a + bi)
Complex operator+(const Complex &lhs, const Complex &rhs)
{
float a = lhs.getReal();
float b = lhs.getImg();
float c = rhs.getReal();
float d = rhs.getImg();
return Complex(a + c, b + d);
}
Complex operator-(const Complex &lhs, const Complex &rhs)
{
float a = lhs.getReal();
float b = lhs.getImg();
float c = rhs.getReal();
float d = rhs.getImg();
return Complex(a - c, b - d);
}
Complex operator*(const Complex &lhs, const Complex &rhs)
{
float a = lhs.getReal();
float b = lhs.getImg();
float c = rhs.getReal();
float d = rhs.getImg();
return Complex(((a * c) - (b * d)) , ((a * d) + (b * c)));
}
Complex operator/(const Complex &lhs, const Complex &rhs)
{
float a = lhs.getReal();
float b = lhs.getImg();
float c = rhs.getReal();
float d = rhs.getImg();
//Numerator
float myReal = (a * c) + (b * d);
float myImg = (b * c) - (a * d);
//Denominator
float myDenom = (pow(c, 2) + pow(d, 2));
return Complex(myReal / myDenom, myImg / myDenom);
}
//
//*****************COMPLEX OPERATORS EQUALS *****************//
//**************** ( += | -= | *= | /= |) ***************** //
//
Complex &Complex::operator+=(const Complex &rhs)
{
real += rhs.real;
img += rhs.img;
//*this = *this + rhs;
return *this;
}
Complex &Complex::operator-=(const Complex &rhs)
{
real -= rhs.real;
img -= rhs.img;
return *this;
}
Complex &Complex::operator*=(const Complex &rhs)
{
real *= rhs.real;
img *= rhs.img;
return *this;
}
Complex &Complex::operator/=(const Complex &rhs)
{
real /= rhs.real;
img /= rhs.img;
return *this;
}
//
//******************COMPLEX OPERATORS COMPARISON*****************//
//**************** ( == | != | < | <= | > | >= |)*****************//
//
bool operator==(const Complex &lhs, const Complex &rhs)
{
if (lhs.getReal() == rhs.getReal() && lhs.getImg() == rhs.getImg())
{
return true;
}
return false;
}
bool operator!=(const Complex &lhs, const Complex &rhs)
{
return !(lhs == rhs);
}
bool operator<(const Complex &lhs, const Complex &rhs)
{
if (lhs.getReal() < rhs.getReal() || lhs.getReal() == rhs.getReal() && lhs.getImg() < rhs.getImg())
{
return true;
}
return false;
}
bool operator<=(const Complex &lhs, const Complex &rhs)
{
return ((lhs < rhs) || (lhs == rhs));
}
bool operator>(const Complex &lhs, const Complex &rhs)
{
return !(lhs <= rhs);
}
bool operator>=(const Complex &lhs, const Complex &rhs)
{
return !(lhs < rhs);
}
Complex operator-(const Complex &lhs)
{
return Complex(-lhs);
}
主文件:
#include <iostream>
#include "com.h"
#include <string>
using namespace std;
int main()
{
Complex userValue;
cout << "Enter a complex value (a + bi): ";
cin >> userValue;
cout << "Negated: " << -userValue << endl;
cout << "Addition: " << userValue + Complex(0.0, 0.0) << endl;
if (Complex(-6, 5.2f) == userValue)
cout << "Wow! It's equal." << endl;
return 0;
}
更新1:哇!我刚刚再次测试了它,它实际上在我输入的任何数字上都崩溃了,阳性和阴性哈哈!但当我评论以下代码时。。。来自主
// cout << "Negated: " << -userValue << endl;
来自头文件
Complex operator-(const Complex &lhs);
来自cpp文件
Complex operator-(const Complex &lhs)
{
return Complex(-lhs);
}
它仍然有效(当然没有负数)
正如错误所说,您正在递归地调用重载函数。相反,您希望使用正确的参数调用构造函数:
Complex operator-(const Complex &lhs)
{
return Complex(-lhs.real, -lhs.imag);
}
相关文章:
- 一元*运算符的操作数是否期望一个 prvalue
- 一元"运算符"未在C++中定义
- 如何解决一元"*"(有"字符")错误的无效类型参数?
- C++11 标准是否保证零值有符号整数的一元减号为零?
- 应用于无符号类型的一元减号运算符
- C2675 一元运算符由错误的作用域引起
- 如何在 std::transform 中使用 std::p ow 作为一元函数?
- 如何解决参数为一元的错误?
- 你能帮我了解重载一元运算符和二进制运算符之间的区别吗?
- 泛型lambda和一元+运算符
- 为什么'auto'不尊重一元减号运算符?
- 在C++中,一元减号运算符如何处理整数文本
- 位集构造函数中的一元按位或折叠
- 错误:一元"*"的类型参数无效(具有"int"):使用 mergesort 计算
- 创建指针是否超过非数组指针的末尾,而不是从 C++17 中的一元运算符和未定义的行为派生?
- 错误 带有模块的一元"*"(具有"int")的类型参数无效
- 为什么我不能定义一元运算符,然后在 MSVC 的模板类中声明具有相同名称的友元二进制运算符?
- 替换包含已弃用的一元函数的自定义模板
- C++一元右折与一元左折,带逗号运算符
- 为什么一元运算符和不需要完整类型?