Type Conversion issue

Hello. I am trying to write a polynomial class that will implement basic polynomial addition, subtraction, and multiplication using input from the user. Right now I am trying to get the program to print the coefficients of the polynomial that the user enters, but I am getting: cannot convert 'double' to 'double*' in assignment. I get errors on lines 93 and 98. Any help would be greatly appreciated.

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
#include <iostream>
using namespace std;

typedef double* dptr;



class Polynomial
{
public:  
//Default constructor
Polynomial( );
//Parametrized constructor
Polynomial(int power);
//Copy constructor
Polynomial(const Polynomial& arrayO);
//Destructor
~Polynomial( );

double getValue(int power);

void setValue(int i, double value);

Polynomial& operator=(const Polynomial& rvalue);
private:
int exponent;
dptr* a;
};

int main() 
{
    int numberOfTerms1, numberOfTerms2;
    double number;
    cout << "Enter the number of terms in the first polynomial.\n";
    cin >> numberOfTerms1;
     cout << "Enter the number of terms in the second polynomial.\n";
    cin >> numberOfTerms2;
    cout << "Enter the values of the coefficients of the first polynomial.\n";
    Polynomial array1(numberOfTerms1);
    
    for (int j=0; j<numberOfTerms1; j++)
    {
        cout << "Enter the value of the " << j << "th term in the polynomial.\n";
        cin >> number;
        array1.setValue(j,number);
    }
    cout << "The values in the first polynomial are: \n";
    for (int j=0; j<numberOfTerms1; j++)
    {
        cout << array1.getValue(j) << " ";
    }
    
    
    
    
    
    
    
    
	return 0;
}

Polynomial::Polynomial( ) : exponent(0)
{
    a = new dptr[exponent];
}

Polynomial::Polynomial(int power) : exponent(power)
{
    a = new dptr[exponent];
}

Polynomial::Polynomial(const Polynomial& arrayO) : exponent(arrayO.exponent), a(arrayO.a)
{
    a = new dptr[exponent];
    for (int i=0; i < exponent; i++)
    {
        a[i]=arrayO.a[i];
    }
}

Polynomial::~Polynomial( )
{
    for (int i=0; i<exponent; i++)
    {
        delete [] a[i];
    }
    delete [] a;
}

void Polynomial:: setValue(int i, double value)
{
    a[i]=value;
}

double Polynomial::getValue(int power)
{
    return a[power];
}

Polynomial& Polynomial::operator=(const Polynomial& rvalue)
{
if (exponent != rvalue.exponent)
{
    for (int i=0; i<exponent; i++)
    {
        delete [] a[i];
    }
    delete [] a;
    
    a=new dptr[rvalue.exponent];
}

exponent = rvalue.exponent;

for (int i=0; i<exponent; i++)
{
    a[i] = rvalue.a[i];
    cout << endl;
}

return *this;

}



Last edited on
I tried modifying it based on the errors I got, but now it outputs zeros when I enter terms for the first polynomial.
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
#include <iostream>
using namespace std;

typedef double* dptr;



class Polynomial
{
public:  
//Default constructor
Polynomial( );
//Parametrized constructor
Polynomial(int power);
//Copy constructor
Polynomial(const Polynomial& arrayO);
//Destructor
~Polynomial( );

void setValue(int i, const Polynomial& value);

double* getValue(int power);

Polynomial& operator=(const Polynomial& rvalue);
private:
int exponent;
dptr* a;
};

int main() 
{
    int numberOfTerms1;
  //  int numberOfTerms2;
    double number;
    cout << "Enter the number of terms in the first polynomial.\n";
    cin >> numberOfTerms1;
  //   cout << "Enter the number of terms in the second polynomial.\n";
  //  cin >> numberOfTerms2;
    cout << "Enter the values of the coefficients of the first polynomial.\n";
    Polynomial array1(numberOfTerms1);
    
    for (int j=1; j<=numberOfTerms1; j++)
    {
        cout << "Enter the value of the " << j << "th term in the polynomial.\n";
        cin >> number;
        array1.setValue(j,number);
    }
    cout << "The values in the first polynomial are: \n";
    for (int j=1; j<=numberOfTerms1; j++)
    {
        cout << array1.getValue(j) << " ";
    }
    
    
    
    
    
    
    
    
	return 0;
}

Polynomial::Polynomial( ) : exponent(0)
{
    a = new dptr[exponent];
}

Polynomial::Polynomial(int power) : exponent(power)
{
    a = new dptr[exponent];
}

Polynomial::Polynomial(const Polynomial& arrayO) : exponent(arrayO.exponent), a(arrayO.a)
{
    a = new dptr[exponent];
    for (int i=0; i < exponent; i++)
    {
        a[i]=arrayO.a[i];
    }
}

Polynomial::~Polynomial( )
{
    for (int i=0; i<exponent; i++)
    {
        delete [] a[i];
    }
    delete [] a;
}

void Polynomial:: setValue(int i, const Polynomial& value)
{
    a[i]=value.a[i];
}

double* Polynomial::getValue(int power)
{
    return a[power];
}

Polynomial& Polynomial::operator=(const Polynomial& rvalue)
{
if (exponent != rvalue.exponent)
{
    for (int i=0; i<exponent; i++)
    {
        delete [] a[i];
    }
    delete [] a;
    
    a=new dptr[rvalue.exponent];
}

exponent = rvalue.exponent;

for (int i=0; i<exponent; i++)
{
    a[i] = rvalue.a[i];
    cout << endl;
}

return *this;

}
Maybe a should just be an array of doubles instead of an array of pointers-to-double.

BTW, it's generally considered a "bad idea" to hide a pointer behind a typedef.
Thank you!
Maybe reconsider the name of the parameter in
Polynomial& Polynomial::operator=(const Polynomial& rvalue)
Because rvalue has a specific technical meaning. In fact, the expression rvalue is actually an lvalue, which makes the name seem misleading at best.

Better names for that parameter include rhs ("right-hand-side") and that (contrast with this), other, or even just r.
Last edited on
Topic archived. No new replies allowed.