- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我不得不把这个程序交作业,它已经打分了,得了 80 分。我要回去清理一些代码。在我的代码底部是重载 = operator
的函数。出于某种原因,我注释掉的 cout
语句导致我的程序崩溃,但是,当我删除//s 时,程序运行正常......
有人知道为什么会这样吗?
#include <iostream>
using namespace std;
class Poly
{
private:
int order; //order of the polynomial
int * coeff;//pointer to array of coeff on the heap
public:
Poly();
Poly(int Order);
Poly(int Order, int * Coeff);
~Poly(){/*cout << "Deconstructor\n";*/};
Poly(const Poly &rhs);
//accessors & mutators
void set();
void set(int * Coeff, int Order);
void Print2();
void PtrReset();
int getorder(){return order;};
int * get()const{return coeff;};
//Overloaded Operators
Poly operator+(const Poly &rhs);
Poly operator-(const Poly &rhs);
Poly operator*(const int scale);
Poly operator=(const Poly &rhs);
Poly operator*(const Poly &rhs);
const int& operator[](int I)const;
int& operator[](int I);
bool operator==(const Poly &rhs);
int operator( )(int X);
friend ostream & operator<<(ostream & Out, const Poly &rhs);
friend istream & operator >>(istream & In, Poly &rhs);
};
int main()
{
int coeff1[ ] = {-19,1,-12,3,1};
int coeff2[ ] = {-19,1,-6,0,0,7,0,-1};
bool flag;
Poly P1(4, coeff1);
Poly P2(7, coeff2);
Poly P3;
P3 = P1 + P2;
cout << "P1 + P2: " << P3;
P3 = P2 - P1;
cout << "P2 - P1: " << P3;
P3 = P1 * 10;
cout << "P1 * 10: " << P3;
P3 = P1 * P2;
cout << "P1 * P2: " << P3;
flag = (P2 == P2);
cout << "P2 and P2 are ";
if (flag)
cout << "Equal\n";
else
cout << "Not Equal\n";
flag = (P1 == P2);
cout << "P1 and P2 are ";
if (flag)
cout << "Equal\n";
else
cout << "Not Equal\n";
cout << "P1(4) = " << P1(4) << endl;
cout << "P1: ";
cout << P1;
cout << "P2: ";
cout << P2;
P1[3] = P2[5];
cout << "P1(after P1[3] = P2[5]): " << P1;
P1[3] = P2[4];
cout << "P1(after P1[3] = P2[4]): " << P1;
return 0;
}
Poly::Poly()
{
coeff = (int *) malloc(sizeof(int));
order = 0;
*coeff = 0;
//cout << "Default Constructor\n";
}
Poly::Poly(int Order)
{
order = Order;
coeff = new int[order+1];
for(int i(0); i <= order; i++)
coeff[i] = 0;
}
Poly::Poly(int Order, int * Coeff)
{
order = Order;
coeff = Coeff;
}
Poly::Poly(const Poly &rhs)
{
order = rhs.order;
int *temp;
temp = (int *) malloc((rhs.order + 1) * sizeof(int));
coeff = (int *) malloc((order + 1) * sizeof(int));
temp = rhs.coeff;
for(int i(0); i <= order; i++)
{
*coeff = *temp;
coeff++;
temp++;
}
PtrReset();
}
void Poly::set()
{
coeff = (int *) malloc((order + 1) * sizeof(int));
if(coeff == NULL)
{
cout << "Error allocating memory!\n";
exit(1);
}
cout << "Begin entering with the non x value, then x^1 coefficient, x^2, etc:\n";
for(int i(0); i <= order; i++)
{
cout << (i+1) << ". ";
cin >> * coeff;
coeff++;
}
PtrReset();
}
void Poly::set(int * Coeff, int Order)
{
order = Order;
for(int i(0); i <= order; i++)
{
*coeff = *Coeff;
coeff++;
Coeff++;
}
PtrReset();
}
void Poly::Print2()
{
for(int i(0); i <= order; i++)
{
cout << * coeff << " ";
coeff++;
}
cout << endl;
PtrReset();
}
void Poly::PtrReset()
{
for(int i(0); i <= order; i++)
coeff--;
}
Poly Poly::operator+(const Poly &rhs)
{
int neworder;
int * newpointer;
int * temp;
temp = rhs.coeff;
if(order >= rhs.order)
{
neworder = order;
newpointer = (int *) malloc((neworder+1) * sizeof(int));
for(int i(0); i <= rhs.order; i++)
{
*newpointer = *coeff + *temp;
newpointer++;
coeff++;
temp++;
}
for(int i(0); i < (order - rhs.order); i++)
{
*newpointer = *coeff;
newpointer++;
coeff++;
}
}
else
{
neworder = rhs.order;
newpointer = (int *) malloc((neworder+1) * sizeof(int));
for(int i(0); i <= order; i++)
{
*newpointer = *coeff + *temp;
newpointer++;
coeff++;
temp++;
}
for(int i(0); i < (rhs.order - order); i++)
{
*newpointer = *temp;
newpointer++;
temp++;
}
}
PtrReset();
for(int i(0); i <= neworder; i++)
newpointer--;
return Poly(neworder, newpointer);
}
Poly Poly::operator-(const Poly &rhs)
{
int neworder;
int * newpointer;
int * temp;
temp = rhs.coeff;
if(order >= rhs.order)
{
neworder = order;
newpointer = (int *) malloc((neworder+1) * sizeof(int));
for(int i(0); i <= rhs.order; i++)
{
*newpointer = *coeff - *temp;
newpointer++;
coeff++;
temp++;
}
for(int i(0); i < (order - rhs.order); i++)
{
*newpointer = *coeff;
newpointer++;
coeff++;
}
}
else
{
neworder = rhs.order;
newpointer = (int *) malloc((neworder+1) * sizeof(int));
for(int i(0); i <= order; i++)
{
*newpointer = *coeff - *temp;
newpointer++;
coeff++;
temp++;
}
for(int i(0); i < (rhs.order - order); i++)
{
*newpointer = 0 - *temp;
newpointer++;
temp++;
}
}
PtrReset();
for(int i(0); i <= neworder; i++)
newpointer--;
return Poly(neworder, newpointer);
}
Poly Poly::operator*(const int scale)
{
int neworder = order;
int * temp = new int[order+1];
for(int i(0); i <= order; i++)
temp[i] = scale * coeff[i];
return Poly(neworder, temp);
}
Poly Poly::operator*(const Poly &rhs)
{
int neworder = order + rhs.order;
int * newcoeff;
int * temp;
temp = rhs.coeff;
newcoeff = (int *) malloc((neworder + 1) * sizeof(int));
for(int i(0); i <= neworder; i++)
{
*newcoeff = 0;
newcoeff++;
}
for(int i(0); i <= neworder; i++)
newcoeff--;
for(int i(0); i <= order; i++)
{
for(int j(0); j <= rhs.order; j++)
{
*newcoeff += (*coeff * *temp);
newcoeff++;
temp++;
}
coeff++;
for(int j(0); j < rhs.order; j++)
newcoeff--;
for(int j(0); j <= rhs.order; j++)
temp--;
}
for(int i(0); i <= (neworder - rhs.order); i++)
newcoeff--;
PtrReset();
return Poly(neworder, newcoeff);
}
bool Poly::operator==(const Poly &rhs)
{
bool test = true;
int count = 0;
while(test && (count <= order))
{
if(order != rhs.order)
test = false;
else if(coeff[count] != rhs.coeff[count])
test = false;
count++;
}
return test;
}
int Poly::operator()(int X)
{
int * temp;
int * temp2;
temp = (int *) malloc((order + 1) * sizeof(int));
temp2 = temp;
for(int i(0); i < order; i++)
coeff++;
for(int i(0); i <= order; i++)
{
*temp = *coeff;
temp++;
coeff--;
}
coeff++;
for(int i(0); i < order; i++)
temp--;
for(int i(0); i < order; i++)
{
*temp += (X * *temp2);
temp++;
*temp2++;
}
return *temp2;
}
ostream &operator <<(ostream& out, const Poly &source)
{
for(int i(source.order); i >= 0; i--)
{
if(i == 1)
{
if(i == source.order)
if(source.coeff[i] == 1)
out << "X";
else if(source.coeff[i] == -1)
out << "-X";
else
out << source.coeff[i] << "X";
else if(source.coeff[i] == 1)
out << " + " << "X";
else if(source.coeff[i] == -1)
out << " - " << "X";
else if(source.coeff[i] > 0)
out << " + " << source.coeff[i] << "X";
else if(source.coeff[i] < 0)
out << " - " << abs(source.coeff[i]) << "X";
}
else if(i > 1)
{
if(i == source.order)
if(source.coeff[i] == 1)
out << "X^" << i;
else if(source.coeff[i] == -1)
out << "-X^" << i;
else
out << source.coeff[i] << "X^" << i;
else if(source.coeff[i] == 1)
out << " + " << "X^" << i;
else if(source.coeff[i] == -1)
out << " - " << "X^" << i;
else if(source.coeff[i] > 1)
out << " + " << source.coeff[i] << "X^" << i;
else if(source.coeff[i] < -1)
out << " - " << abs(source.coeff[i]) << "X^" << i;
}
else
{
if(source.coeff[i] > 0)
out << " + " << source.coeff[i];
else if(source.coeff[i] < 0)
out << " - " << abs(source.coeff[i]);
}
}
out << endl;
return out;
}
int& Poly::operator[](int I)
{
if(I > (order + 1) || I < 0)
{
cout << "Request to access outside Poly boundaries!" << endl;
exit(1);
}
return coeff[I];
}
const int& Poly::operator[](int I) const
{
if(I > (order + 1) || I < 0)
{
cout << "Request to access outside Poly boundaries!" << endl;
exit(1);
}
return coeff[I];
}
Poly Poly::operator=(const Poly &rhs)
{
order = rhs.order;
//cout << "new order = " << rhs.order << endl; ******HERE*******
for(int i(0); i <= rhs.order; i++)
coeff[i] = rhs.coeff[i];
return Poly(order, coeff);
}
最佳答案
赋值运算符
作为成员应该返回对this
的引用如果它不会,那么您可能不应该使用赋值运算符
。
撇开注释行不谈,您的赋值运算符的另一个直接问题是,当您手动构建一个新的 coeff 数组时,您会泄漏更多的内存。您还返回了第三个对象,这不是任何使用 assignment operator
的人所期望的,这是彻头彻尾的危险
Poly& Poly::operator=(const Poly &rhs)
{
order = rhs.order;
// you should probably initialize coeff here
//cout << "new order = " << rhs.order << endl; ******HERE*******
for(int i(0); i <= rhs.order; i++)
coeff[i] = rhs.coeff[i];
//This is wrong return this. see signature fix above
//return Poly(order, coeff);
return *this;
其次,与其手动处理您在代数运算符中所做的任何事情,不如首先复制构造,然后修改该对象。
例如乘法:
//member multiplication which still sucks because we are going to copy construct again to return..
Poly Poly::operator*(const int scale)
{
Poly result(*this);
// now manipulate your new object and leave THIS this alone
for(int i(0); i <= result.order; i++)
result.coeff[i] *= scale;
return result;
}
如果你把它变成一个非成员函数,然后创建一个成员函数 *=operator
就更好了,然后你就可以像 Ben 和我设想的那样
Poly operator*(Poly result, int scale)
{
return (result *= scale);
}
我看到的主要问题是你正在疯狂地泄漏内存并且从来没有对其进行任何边界保护。
关于c++随机cout语句导致程序在重载的 '='运算符内崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17982241/
假设我有一个类,我在其中重载了运算符 == : Class A { ... public: bool operator== (const A &rhs) const; ... };
我知道你不应该使用 std::find(some_map.begin(), some_map.end()) 或 std::lower_bound,因为它会采用线性时间而不是 some_map.lowe
我正在尝试在 Haskell 中定义 Vector3 数据类型,并允许在其上使用 (+) 运算符。我尝试了以下方法: data Vector3 = Vector3 Double Double Doub
我已经为我的类图将运算符重载为“-”。它的用途并不完全直观(糟糕的编码 - 我知道)但是如果我做 graph3 = graph2-graph1 那么图 3 是应该只接收图 2 和图 1 中的那些顶点。
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Operator overloading 我想重载 以按字母顺序排列字符串,但我不确定该怎么做。 如何再次
下面的代码给我一个编译错误。谁能告诉我为什么? class mytype { public: int value; mytype(int a) { value = a;
这有什么问题吗? class Vec2 attr_accessor :x, :y # ... def += (v) @x += v.x @y += v.y retu
是否可以重载 [] 运算符两次?允许这样的事情:function[3][3](就像在二维数组中一样)。 如果可能的话,我想看看一些示例代码。 最佳答案 您可以重载 operator[] 以返回一个对象
我的团队目前正在与 Lua 合作,创建一个 android 游戏。我们遇到的一件事是表面上无法创建重载构造函数。 我习惯于使用默认值设置一个对象,然后在需要时使其过载。 前任: apples() {
我有一个网页,在某个时候显示一个导航栏,它只不过是一个 a 元素的列表 (ul)。所述 a 元素的大多数样式规则都是通用的。唯一应该改变的部分是要显示的图像,可以从列表中每个 li 元素的 id 标签
我对使用/重载“范围步长”运算符(.. ..)很感兴趣,但我终其一生都无法了解如何使用它。 在文档中它说 // Usage: start .. step .. finish 但是在 F# shell
Java 11(可能无关紧要): public static String toString(Object obj) { return ReflectionToStringBuilder.to
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我无法理解以下代码(针对行号进行注释) class Base { void m1(Object o) { } void m2(String o) { } } publi
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
class myclass{ //definitions here }; myclass e; int myarray[10]; /* Do something... */ e = myarray;
为什么不能将下标运算符(operator [])作为 friend 函数重载? 最佳答案 正如Bjarne Stroustrup在D&E book中所说: However, even in the o
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
因此,我有一个问题是我最近尝试重载 namespace Eng { /** * A structure to represent pixels */ typedef
如何重载onResume()以正确的方式工作?我想从 activity 返回到 MainActivity ,我希望在其中具有与应用程序启动后相同的状态。我想使用 recreate() 但它循环了或者类
我是一名优秀的程序员,十分优秀!