Overloading == Operator and std bad_alloc error

I've been trying to code for this question, where I have to add and compare two polynomials. Issues I have been facing are, with the compare function, whether the two polynomials I enter are equal or not, I get an output saying they are equal. And with overloading the + operator, I get the std bad_alloc error.

Would really appreciate help with this! I'm using the g++ compiler and I have also tried this in codeblocks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include<stdio.h>
#include<iostream>

using namespace std;
class polynomial{
private:
  int *a;
  int sizert;
public:
  
  void display()
  {
    int i=0;
    for(i=0;i<sizert;i++)
      cout<<a[i]<<" ";
  }
  
  polynomial()
  {
    a=NULL;
    sizert=0;
  }
  polynomial(int x[], int p)
  {
    sizert=p;
    if(sizert==0)
    {
      a=NULL;
      
    }
    else
    {
      a=new int[sizert];
      for(int i=0;i<sizert;i++)
	a[i]=x[i];
    }
  }
  polynomial(const int x)
  {
    a=new int[sizert];
    for(int i=0;i<sizert;i++)
    a[i]=0;
    a[0]=(int)x;
  }
  polynomial(polynomial& ss)
  {
    
    if(ss.sizert==0)
    {
      a=NULL;
      
      sizert=0;
    }
    else
    {
      a=new int[sizert];
      
    }
    for(int i=0;i<ss.sizert;i++)
      a[i]=ss.sizert;
   
    sizert=ss.sizert;
  }
  
  ~polynomial()
  {
    delete[] a;
    sizert=0;
  }
  
  
  polynomial operator + (polynomial t)
  {
    polynomial d;
    d.a=new int[sizert];
    int i=0;
    for(i=0;i<sizert;i++)
    {
      d.a[i]==t.a[i]+d.a[i];
    }
    return d;
  }
  
   int operator == (polynomial t)
  {
    int i=0;
    for(i=0;i<sizert;i++)
    {
      if(a[i]==t.a[i])
	continue;
      else 
	return 0;
    }
    return 1;
  }
  
  template<class T>
  int compare(T x, T y)
  {
    if(x==y)
      return 1;
    else
      return 0;
  }

  


};
  
  
  int main()
  {
    int a[10],b[10],sizert;
    cout<<"Enter the highest degree of x\n";
    cin>>sizert;
    cout<<"For polynomial ONE\n";
    int i=0;int j=0;
    for(i=0;i<sizert;i++)
    {
      cout<<"Enter the coefficient of x power "<<i;
      cin>>a[i];
    }
    cout<<"For polynomial TWO\n";
    for(j=0;j<sizert;j++)
    {
      cout<<"Enter the coefficient of x power "<<j;
      cin>>b[i];
    }
    
    polynomial r(a,sizert);
    polynomial f(b,sizert);
    
    int y=r.compare(r,f);
    if(y==1)
      cout<<"Equal";
    else
      cout<<"Unequal";
    polynomial g;
    g=r+f;
    g.display();
    return 0;
    
    
  }
  
    
    
    



Thansk and Regards!
Saaketh
Still going through your code, but I noticed in line 40 that you are using sizert before it's initialized.

Same with line 56.

And line 60 should look like line 35.
Last edited on
Now on to your question.

First of all, I would pass t by const& in both 72 and 84.

Next, in operator==, you need to make sure that the polynomials are the same size.

Next, in operator+, you need to make sure that only populated elements in the accessed polynomials are used. Don't go past the bounds. And if the polynomials are not the same size, you need to figure out if you need to skip elements at the beginning or at the end when adding them up (probably at the beginning).

Line 70 has problems. First of all, == is a comparison, not an assignment. Also, you should be adding
d.a[i] = t.a[i] + a[i].

There's probably more, but that's enough for now.

Edit: one more--line 134 should just be
int y = (r == f);.
You don't need the template function.
Last edited on
The only error i am getting is
"terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc"
I'm not exactly sure shy you are getting a std::bad_alloc error. You should probably run your code through a debugger to find out when the bad_alloc occurs.

In my previous post, I mentioned line 70. My eyesight must be going bad because I really meant line 79 (it looked like 70 to my eyes). Since your code is failing in operator+(), that's a good place to start.
Topic archived. No new replies allowed.