C ++中的重载运算符

编程入门 行业动态 更新时间:2024-10-24 16:24:56
本文介绍了C ++中的重载运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想为多项式函数创建一个类.因此,该类的一个功能应该是对2个多项式函数求和,这样我就应该为该类重载+运算符.我写了这段代码,但出现错误: 表达式:_BLOCK_TYPE_IS_VALID 我的代码有什么问题?

Hi, I want to make a class for polynomial functions. So one feature of this class should be to sum 2 polynomial function so I should overload + operator for the class. I wrote this code but I got error: Expression: _BLOCK_TYPE_IS_VALID What''s wrong in my code?

#include <iostream> #include <conio.h> using namespace std; //Polynomial Class class Polynomial { public: int *factor; int *power; unsigned int n; Polynomial() { n = 0; } ~Polynomial() { delete[] factor; delete[] power; } private: void Sort(); void swap(int &x, int &y); public: void MakePolynomial(unsigned int NumberofTerms); void Print(); Polynomial operator +(Polynomial p); Polynomial operator -(Polynomial p); void Search(); bool operator ==(Polynomial p); char *CalDerived(); }; void Polynomial::Sort() { int last = (n + 1) - 2; int isChanged = 1; while (last >= 0 && isChanged) { isChanged = 0; for (int k = 0; k <= last; k++) { if (power[k] < power[k+1]) { swap(power[k], power[k+1]); swap(factor[k], factor[k+1]); isChanged = 1; } last--; } } } void Polynomial::swap(int &x, int &y) { int temp; temp = x; x = y; y = temp; } void Polynomial::MakePolynomial(unsigned int No) { factor = new int[No]; power = new int[No]; } void Polynomial::Print() { for(int i = 0; i < n; i++) { if(factor[i] >= 0) { cout << "+" << factor[i] << "x^" << power[i]; } else { cout << factor[i] << "x^" << power[i]; } } } Polynomial Polynomial::operator +(Polynomial p) { Polynomial temp; temp.MakePolynomial(3); for(int i = 0; i < n; i++) { temp.factor[i] = factor[i] + p.factor[i]; temp.power[i] = power[i]; } return temp; } /* Polynomial Polynomial::operator -(Polynomial p) { } */ void main() { Polynomial p, p2, sum; p.MakePolynomial(3); p.factor[0] = 2; p.power[0] = 0; p.factor[1] = -4; p.power[1] = 1; p.factor[2] = 3; p.power[2] = 2; p.n = 3; p.Print(); cout << endl << "+\n"; p2.MakePolynomial(3); p2.factor[0] = 3; p2.power[0] = 0; p2.factor[1] = 4; p2.power[1] = 1; p2.factor[2] = 7; p2.power[2] = 2; p2.n = 3; p2.Print(); cout << endl << "=\n"; sum.MakePolynomial(3); sum.n = 3; sum = p + p2; sum.Print(); getch(); } </conio.h></iostream>

感谢

Thanks

推荐答案

在哪里以及何时出现该错误? 我可以看到的唯一实际问题是您没有提供赋值运算符,因此编译器将提供它自己的赋值运算符,并且该函数将简单地复制所有成员,包括指针.该默认赋值运算符将在以下行中调用: Where and when do you get that error? The only actual problem I can see is that you didn''t provide an assignment operator, so the compiler will provide it''s own, and that one will simply copy all members, including the pointers. This default assignment operator will be called in this line: sum = p + p2;

这里的问题是现在将发生这种情况: 1. operator+将创建一个新的临时Polynomial对象 2.默认赋值运算符会将所有数据从该临时对象复制到sum,包括指针 factor和power. 3.临时对象将被销毁,因此其数组power和factor将被删除. 4.现在sum.factor和sum.power指向无效的内存! 5.调用sum.Print()可能会失败 6.即使没有,一旦程序退出,sum也将被破坏,尝试尝试delete[] power和factor,从而导致运行时错误. 解决方案:创建您自己的赋值运算符,该赋值运算符实际上复制数组而不是指针. operator+的实现中还存在另一个问题:您假设两个操作数具有(不超过3个)项,并且这些项具有相应的幂值.您要么从数据结构中完全删除电源,然后以预定义的顺序保存因数",要么​​必须比较电源值,然后仅对对应的电源值和您必须考虑以下可能性:将两个具有3个因数的多项式相加时,所得多项式可能需要存储多达6个因数!

The problem here is that now this will happen: 1. operator+ will create a new, temporary Polynomial object 2. The default assignment operator will copy all data from that temporary object to sum, including the pointers factor and power. 3. The temporary object will be destroyed, and therefore its arrays power and factor will be deleted. 4. Now sum.factor and sum.power point to invalid memory! 5. the call to sum.Print() will likely fail 6. Even if not, once the program exits, sum will be destructed, trying to delete[] power and factor, causing a runtime error. Solution: Create your own assignment operator that actually copies the arrays rather than the pointers. There''s also another issue in the implementation of operator+: you are assuming that the two operands have (no more than) 3 terms, and that these terms have corresponding values for power. Either you drop power from your data structure entirely and just save your ''factor''s in a predefined order, or else you must compare the power values and up only the corresponding ones, and you must consider the possibility that upon adding two polynomials with 3 factors each, the resulting polynomial might need to store up to 6 factors!

其他问题包括您的未初始化因数和构造函数中的power为0,导致以下未定义: Other problems include your not initializing factor and power to 0 in the constructor making the following undefined: { Polynomial p; // constructed } // destructed, and most likely with an access violation

更多推荐

C ++中的重载运算符

本文发布于:2023-06-03 12:16:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/473743.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:运算符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!