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.
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.
And how do you do what Polynomial operator +(Polynomial& one, Polynomial& two)
as I know it must be Polynomial Polynomial::operator +(const Polynomial& one)
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...
@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.
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.
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.
//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 = newdouble[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 = newdouble[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 = newdouble[3];
memcpy(p, op2.p, 3 * sizeof(double));
return *this;
}