我在VS上有以下代码,用于编写多项式计算器类。 .h文件如下
#pragma once
#include <stdio.h>
#include <iostream>
using namespace std;
class Poly {
private:
int coefficient=0;
int largest_exponent=0;
int* array = nullptr;
//string input;
public:
//constructor
Poly(int coefficient, int largest_exponent);
Poly(int coefficient); // assume the exponent or power is 0
Poly(); // empty constructor 0 coeffecient and 0 exponent
Poly(const Poly& poly); //copy constructor
//destructoer
~Poly();
int getCoeff(int num);
void setCoeff(int coeff, int exp);
//delete and valgrind
//Overload
Poly operator*(const Poly &poly) const;
Poly operator+(const Poly &poly) const;
//Poly operator+(const Poly &poly);
Poly operator-(const Poly &poly) const;
Poly operator*=(const Poly &poly) const;
Poly operator+=(const Poly &poly) const;
Poly operator-=(const Poly &poly) const;
Poly operator=(const Poly &poly) const;
Poly operator==(const Poly &poly) const;
Poly operator!=(const Poly &poly) const;
friend ostream& operator<<(ostream &os, const Poly &poly);
friend istream& operator>>(istream &is, Poly &poly);
};
我的.cpp文件如下
#include "pch.h"
#include "Poly.h"
Poly::Poly(int coefficient, int largest_exponent) { // largest component must be +ve value
this->largest_exponent = largest_exponent;
this->coefficient = coefficient;
this->array = new int[largest_exponent+1];
for (int i = 0; i <= largest_exponent; i++) {
this->array[i] = 0; // Initialize all elements to zero.
}
this->array[largest_exponent] = coefficient;
}
Poly::Poly(int coefficient) { // assume the exponent or power is 0
this->largest_exponent = 0;
this->coefficient = coefficient;
this->array = new int[1];
for (int i = 0; i <= 0; i++) {
this->array[i] = 0; // Initialize all elements to zero.
}
this->array[0] = coefficient;
}
Poly::Poly() { // empty constructor 0 coeffecient and 0 exponent
this->coefficient = 0;
this->largest_exponent = 0;
//this->array = new int[1];
this->array = new int[2];
for (int i = 0; i <= 0; i++) {
this->array[i] = 0; // Initialize all elements to zero.
}
}
Poly::Poly(const Poly& poly) { //copy constructor
this->coefficient = poly.coefficient;
this->largest_exponent = poly.largest_exponent;
this->array = new int[poly.largest_exponent+1];
for (int i = 0; i <= poly.largest_exponent; i++) {
this->array[i] = poly.array[i]; // Initialize all elements to zero.
}
}
Poly::~Poly() {
cout << "deleting Polynomial array" << endl;
delete[] array;
array = nullptr; //or delete array;
}
int Poly::getCoeff(int num) {
int coeff = this->array[num];
if (coeff > -33686019) {
cout << "coeff of power " << num << " of P is: " << coeff << endl;
return this->array[num];
}
else {
cout << "this Exponent does not exist in the polynomial" << endl;
return 0;
}
}
void Poly::setCoeff(int coeff, int exp) {
int* newarray = new int[(abs(exp)) + 1];
for (int i = 0; i <= (abs(exp));i++) {
newarray[i] = 0;
}
for (int i = 0; i <= this->largest_exponent;i++) {
newarray[i] = this->array[i];
}
newarray[(abs(exp))] = coeff;
this->array = newarray;
//this->array[abs(exp)] = coeff;
if ((abs(exp)) > this->largest_exponent) {
this->largest_exponent = (abs(exp));
}
}
ostream& operator<<(ostream &os, const Poly &poly) {
bool flag = false;
for (int i = poly.largest_exponent; i >= 0; i--) {
if (poly.array[i] > 0 && i > 1) {
os << " +" << poly.array[i] << "x^" << i;
flag = true;
}
if (poly.array[i] < 0 && i > 1) {
os << " " << poly.array[i] << "x^" << i;
flag = true;
}
if (poly.array[i] > 0 && i == 1) {
os << " +" << poly.array[i] << "x";
flag = true;
}
if (poly.array[i] < 0 && i == 1) {
os << " " << poly.array[i] << "x";
flag = true;
}
if (poly.array[i] > 0 && i == 0) {
os << " +" << poly.array[i];
flag = true;
}
if (poly.array[i] < 0 && i == 0) {
os << " " << poly.array[i];
flag = true;
}
}
if (flag == false) {
os << " 0";
}
return os;
}
istream& operator>>(istream &is, Poly &poly) {
poly.largest_exponent = 0;
int * temparray=new int[0];
int coeff;
int exp;
cout << "Enter terms for polynomial A. Enter a coefficient " << endl;
if (std::cin >> coeff >> exp) {
if (coeff != -1 && exp != -1) {
poly.array[exp] = coeff;
if (poly.largest_exponent < coeff)
poly.largest_exponent = coeff;
}
}
return is;
}
Poly Poly::operator*(const Poly &poly) const {
Poly * temp = new Poly();
temp->largest_exponent = poly.largest_exponent + this->largest_exponent;
for (int k = 0; k <= temp->largest_exponent; k++) {
temp->array[k] = 0; // Initialize all elements to zero.
}
for (int i = 0; i <= this->largest_exponent; i++) {
for (int j = 0; j <= poly.largest_exponent; j++) {
temp->array[i + j] += (this->array[i] * poly.array[j]);
}
}
return *temp;
}
Poly Poly::operator+(const Poly &poly) const {
Poly * temp = new Poly();
int smallest_exponent;
if (this->largest_exponent >= poly.largest_exponent) {
temp->largest_exponent = this->largest_exponent;
smallest_exponent = poly.largest_exponent;
for (int i = 0; i <= temp->largest_exponent; i++) {
temp->array[i] = this->array[i];
}
}
else {
temp->largest_exponent = poly.largest_exponent;
smallest_exponent = this->largest_exponent;
for (int i = 0; i <= temp->largest_exponent; i++) {
temp->array[i] = poly.array[i];
}
}
for (int i = 0; i <= smallest_exponent; i++) {
temp->array[i] = poly.array[i] + this->array[i];
}
return *temp;
}
Poly Poly::operator-(const Poly &poly) const {
Poly * temp = new Poly();
int smallest_exponent;
if (this->largest_exponent >= poly.largest_exponent) {
temp->largest_exponent = this->largest_exponent;
smallest_exponent = poly.largest_exponent;
for (int i = 0; i <= temp->largest_exponent; i++) {
temp->array[i] = this->array[i];
}
}
else {
temp->largest_exponent = poly.largest_exponent;
smallest_exponent = this->largest_exponent;
for (int i = 0; i <= temp->largest_exponent; i++) {
temp->array[i] = poly.array[i];
}
}
for (int i = 0; i <= smallest_exponent; i++) {
temp->array[i] = this->array[i] - poly.array[i];
}
if (this->largest_exponent < poly.largest_exponent) {
for (int i = smallest_exponent+1; i <= poly.largest_exponent; i++) {
temp->array[i] = temp->array[i]*-1;
}
}
return *temp;
}
这是我的驱动程序.cpp
#include "pch.h"
#include <iostream>
#include "Poly.h"
int main()
{
Poly A(-5,1);
int x = A.getCoeff(1);
A.setCoeff(7,3);
A.setCoeff(6,2);
Poly B(2);
B.setCoeff(8,2);
B.setCoeff(8,0);
Poly C;
Poly D(A);
cout << A << endl;
cout << B << endl;
cout << C << endl;
cout << D << endl;
Poly E = B + A;
cout << E << endl;
Poly F = A-B;
cout << F << endl;
Poly G = B-A;
cout << G << endl;
Poly H = B * A;
cout << H << endl;
cout << endl;
//Poly I;
//cin >> I;
//cout << I << endl;
return 0;
}
每当尝试运行此代码时,即使单击继续,结果仍会显示以下错误。有人知道我在哪里弄乱内存吗?
调试错误!
程序:
检测到堆损坏:在正常块(#195)之后的0x00A03A10。 CRT检测到应用程序在堆缓冲区结束后写入了内存。
(按“重试”以调试应用程序) 检测到严重错误c0000374 Poly.exe触发了断点。
在Poly.exe中的0x77DC61E4(ntdll.dll)处引发的异常:0xC0000374:堆已损坏(参数:0x77DE2378)。 Poly.exe中0x77DC61E4(ntdll.dll)处未处理的异常:0xC0000374:堆已损坏(参数:0x77DE2378)。
'Poly.exe'(Win32):已加载'C:\ WINDOWS \ SysWOW64 \ user32.dll'。找不到或打开PDB文件。 'Poly.exe'(Win32):已加载'C:\ WINDOWS \ SysWOW64 \ gdi32.dll'。找不到或打开PDB文件。 'Poly.exe'(Win32):已加载'C:\ WINDOWS \ SysWOW64 \ imm32.dll'。找不到或打开PDB文件。 'Poly.exe'(Win32):已加载'C:\ WINDOWS \ SysWOW64 \ msctf.dll'。找不到或打开PDB文件。 'Poly.exe'(Win32):已加载'C:\ WINDOWS \ SysWOW64 \ uxtheme.dll'。找不到或打开PDB文件。 'Poly.exe'(Win32):已加载'C:\ WINDOWS \ SysWOW64 \ combase.dll'。找不到或打开PDB文件。 'Poly.exe'(Win32):已加载'C:\ WINDOWS \ SysWOW64 \ dwmapi.dll'。找不到或打开PDB文件。 'Poly.exe'(Win32):已加载'C:\ WINDOWS \ SysWOW64 \ ole32.dll'。找不到或打开PDB文件。 'Poly.exe'(Win32):已加载'C:\ WINDOWS \ SysWOW64 \ kernel.appcore.dll'。找不到或打开PDB文件。 在Poly.exe中的0x77D21796(ntdll.dll)处引发了异常:0xC0000005:访问冲突读取位置0x00000000。
答案 0 :(得分:1)
您的代码包含很多错误。
首先,如果所有运算符不是常数,则不要将它们标记为const
。赋值运算符永远不是常数。
第二,您根据构造函数为array
成员分配不同的大小。最好的做法是在尝试访问数组成员以验证索引没有限制时执行assert
。
接下来,在您的setCoef(...)
中,将旧数组的内容复制到新数组中,如果新数组小于旧数组,则会出现访问冲突:
...
void Poly::setCoeff(int coeff, int exp) {
int* newarray = new int[(abs(exp)) + 1];
for (int i = 0; i <= (abs(exp));i++) {
newarray[i] = 0;
}
for (int i = 0; i <= this->largest_exponent;i++) {
newarray[i] = this->array[i]; // HERE <<<<<<<<<<<<
}
...
此外,当您重新分配之前的delete[]
array
时,您不会setCoef(...)