line 33
return Array(ptr);
constructs an Array object by passing 'ptr' to the ctor. Your ctor takes an
int
, but 'ptr' is an
int*
, which is why you get the error.
However this is just a tangent of your real problem: your assignment operator is a bit flawed:
1) it should take a const reference as the param, not a non-const (ie:
const Array& dizi
).
2) it should return a
reference to 'this', and should not construct an entirely separate object.
3) if should
make a copy of the contained array, rather than simply duplicating the pointer. Consider that if you have two Array objects 'a' and 'b', and assign a=b; Then when a's destructor runs it will delete[] the buffer, and when b's destructor runs it will delete[]
the same buffer (since they share the same pointer) -- this will most likely cause your program to crash.
4) The same problem as #3 will happen with the default copy ctor, which you should overload as well.
5) You'll need to protect against self-assignment (ie: "a=a;").
Here's a more typical/functional assignment operator (note there are optimizations you can make, but I'm trying to keep the example simple):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
Array& operator = (const Array& dizi)
{
// protect against self assignment
if(&dizi == this)
return *this;
// kill the original buffer
delete[] ptr;
// re-allocate the buffer
size = dizi.size;
ptr = new int[size];
// copy the buffer contents
for(int i = 0; i < size; ++i)
ptr[i] = dizi.ptr[i];
// all done -- return reference to this
return *this;
}
|
The copy ctor can make use of the assignment operator -- just be careful about that delete[]! Don't want to delete a bad pointer:
1 2 3 4 5
|
Array(const Array& dizi)
{
ptr = 0; // make it a null pointer so delete[] has no effect
*this = dizi; // then copy
}
|