Copy constructor, returning pointer

Hi guys,

Need a little help with a polynomial class. The function "Polynomial operator +(Polynomial& one, Polynomial& two)" makes my program crash. I have tried many different things, but with little success. I need to return a pointer object of the class Polynomial, and then print it. The problem has probably something to do with syntax, but I am not sure.

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

// Visual_Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <string>
#include <cctype>
#include <vector>
#include <fstream>
using namespace std;
using std::ifstream;
using std::ofstream;
using std::endl;
using std::ios;

const int FUNCTION=159;

class Polynomial
{
public:
	Polynomial::Polynomial();
	Polynomial::~Polynomial();
	Polynomial::Polynomial(double *a);
	Polynomial::Polynomial(const Polynomial& a);

	void Polynomial::input();
	void Polynomial::print();
    friend Polynomial operator +(Polynomial& one, Polynomial& two);
	
private:
	int degree;
	double *p;
	
	char Polynomial::convert_int(int i);
	void Polynomial::reverse_polynomial(double *a);
	void Polynomial::switch_numbers(double& a, double& b);
	int Polynomial::get_degree();
};

int main()
{	
	Polynomial one,two, addition;
	char choice;
	do
	{
		do
		{
			cout<<"Enter your first polynomia\n\n";
			one.input();
			cout<<endl;
			one.print();
			cout<<"Is this the correct polynomial (y/n): ";
			cin>>choice;
		}while(choice=='n' || choice=='N');

		do
		{
			cout<<"Enter your second polynomial\n\n";
			two.input();
			cout<<endl;
			two.print();
			cout<<"Is this the correct polynomial (y/n): ";
			cin>>choice;
		}while(choice=='n' || choice=='N');

		cout<<"After addition, you have: ";
		addition=one+two;
		addition.print();


		cout<<"Would you like to do more polynomial operations? (y/n): ";
		cin>>choice;
	}while(choice=='y' || choice=='Y');

	_getch();
}
Polynomial::Polynomial(){}
Polynomial::Polynomial(const Polynomial& a){}
Polynomial::Polynomial(double *a)
{
	for(int i=0;i<3;i++)
		cout<<a[i]<<" ";


}
Polynomial::~Polynomial()
{
	delete [] p;
}
void Polynomial::input()
{
	int counter=0;
	double next;
	cout<<"Enter the highest degree of the polynomail you wish to enter: ";
	cin>>degree;
	while(degree<0)
	{
		cout<<"You cannot have a negative degree, please enter a positive number: ";
		cin>>degree;
	}
	p=new double[degree];
	cout<<"Starting with the constant, enter the coefficients\n\n";
	while(counter<degree+1)
	{	
		if(counter==1)
			cout<<"Enter the "<<counter<<"st degree: ";
		else if(counter==2)
			cout<<"Enter the "<<counter<<"nd degree: ";
		else
			cout<<"Enter the "<<counter<<"th degree: ";
		cin>>next;
		p[counter]=next;
		counter++;
	}
	reverse_polynomial(p);
	cout<<endl<<endl;
}
void Polynomial::print()
{
	char temp=convert_int(FUNCTION);
	cout<<temp<<"(X):=";
	for(int i=0;i<degree+1;i++)
	{
		if(p[i]<0 || p[i]==degree)
			cout<<p[i]<<"(X^"<<degree-i<<")";
		else if(p[i]==degree+1)
			cout<<p[i]<<"(X^"<<degree-i<<")";
		else
			cout<<"+"<<p[i]<<"(X^"<<degree-i<<")";
	}
	cout<<endl<<endl;
}
void Polynomial::switch_numbers(double& a, double& b)
{
	double temp;
	temp=a;
	a=b;
	b=temp;
}
void Polynomial::reverse_polynomial(double *a)
{
	double temp;
	for(int i=0;i<degree/2;i++)
		switch_numbers(a[i],a[degree-i]);
}
char Polynomial::convert_int(int i)
{
	return (char)i;
}
int Polynomial::get_degree()
{
	return degree;
}
Polynomial operator +(Polynomial& one, Polynomial& two)//////////////////////Problem here!!!!!?//////////////////////////////////////////
{
	double *solution=new double[3];//don't pay attention to the number 3 //it's a temporary holder until I can get the remainder of the function working

	for(int i=1;i<3;i++)
	{
		solution[i]=one.p[i]+two.p[i];
		cout<<solution[i]<<" ";
	}	
		
	return Polynomial(solution);//////////////////////Problem here!!!!!?//////////////////////////////////////////
}
I think you code crashes on print(), not the constructor that takes a double* or the copy constructor.
Polynomial::Polynomial(double *a);
doesn't return a thing... so

 
return Polynomial(solution);


doesn't return anything... an error happened...

a copy constructor probably like this:
1
2
3
4
void Polynomial::operator= (Polynomial b) {
	for (int i = 0; i < 3; ++i)
		p[i] = b.p[i];
}


and constructor doesn't return anything

CMIIW
Huh?? Chipp, that is an operator=, not a constructor. But good thing you mention the assignment operator. Since line 72 is assigning (not constructing), a suitable assignment operator should exist.

Still, @chipp, what you quote there is just the constructor prototype. The definition is in line 84. Plus, constructors don't have return statements, so I'm unsure about what you are trying to say here.
give to us "stdafx.h" file. Can't compile without it.
and this is wrong
1
2
3
4
5
6
7
8
public:
	Polynomial::Polynomial();
	Polynomial::~Polynomial();
	Polynomial::Polynomial(double *a);
	Polynomial::Polynomial(const Polynomial& a);

	void Polynomial::input();
	void Polynomial::print();


must be
1
2
3
4
5
6
7
8
public:
	Polynomial();
	~Polynomial();
	Polynomial(double *a);
	Polynomial(const Polynomial& a);

	void input();
	void print();


And how do you do what
Polynomial operator +(Polynomial& one, Polynomial& two)
as I know it must be
Polynomial Polynomial::operator +(const Polynomial& one)
Last edited on
oh yeah, sorry, i just noticed that we're talking about constructor and not operator... -_-! because the "copy" words makes me think about operator= and ah, i just realized probably because he writes it using operator+ and not constructor. but he can modified the operator= function become copy constructor based on my example... it has the same pattern...

for this

 
Polynomial::Polynomial(double *a);


i just want to say that the function Polynomial(double *a) doesn't return a thing. so i'm talking about the function's name... (i just recklessly slap the whole code)

and the last line is just an addition, i just want to remind that constructor doesn't return a thing...
Last edited on
And int main() don't have a return 0;
@TC: There is a crash because the constructor you invoke to create the Polynomial you will return doesn't actually write to the internal array. So when you go to access the pointer it's uninitialized.

@Shinigami: Interestingly, that isn't required. It's assumed at the end even if you omit it.
@Zhuge: yes you are right, but it is bad programing style.

Can someone please show me what the correct syntax/code is inside the copy constructor so I can perform the desired operation(s) without the program crashing? This is all very new to me and confusing, especially with pointers.

Thanks,

Mike

toomanystars: To sum it up, I don't see a problem with your constructors. I believe your code crashes on the call to print() on line 73 because print() accesses the internal p variable, and your constructors don't allocate memory for this p variable.

So what you need to do is correctly fill p on construction, copy construction and assignment. You currently have 2 constructors, 1 copy constructor and zero assignment operators. You must address the internal state of all member fields of the class whenever either constructor or assignment operator is called so your program doesn't crash. Your Polynomial class has 2 member fields: degree and p. You must ensure you have valid data in them at all times, and construction is the first place you must ensure this.

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
//This is how you give the variables some default values.
//Note how the pointer is NULL (zero), or nullptr in C++11.  This is customary.
Polynomial::Polynomial() : degree(0), p(NULL) //or p(0), or p(nullptr)
{ } //No additional initialization code required, so constructor body remains empty.
//Now your other constructor.  You receive what I would imagine is the contents of p.
Polynomial::Polynomial(double *a) : degree(0)
{
    //We don't know the block size pointed to by a.  You assume a magic number of 3.  Will do the same.
    //First allocate memory of your own.
    p = new double[3];
    //Now copy contents.  Can be a FOR loop, but I like memcpy().
    memcpy(p, a, 3 * sizeof(double));
}
//Now copy construction.
Polynomial::Polynomial(const Polynomial &src) : degree(src.degree), p(src.p)
{
    if (!src.p) return;  //Don't try to perform a copy if there's no data to copy.
    p = new double[3];
    memcpy(p, src.p, 3 * sizeof(double));
}
//Finally, assignment operator.  It is a mixture of construction and destruction code.
Polynomial& Polynomial::Polynomial operator=(const Polynomial &op2)
{
    degree = op2.degree;
    if (p) delete[] p;
    p = op2.p;
    if (!op2.p) return;
    p = new double[3];
    memcpy(p, op2.p, 3 * sizeof(double));
    return *this;
}
Last edited on
Topic archived. No new replies allowed.