I'm attempting to overload the = operator for a class designed to manage arrays of integers (inb4 vectors). But anytime there is an '=' operation in the main file I get the following error:
1 2
main.cpp:29: error: assignment of function ‘IntArray A()’
main.cpp:29: error: cannot convert ‘IntArray’ to ‘IntArray()’ in assignment
You're probably wrong about this. You're probably declaring either A or B as a function prototype and not as an instance.
1 2
IntArray A(); // a FUNCTION PROTOTYPE, not an object
IntArray B; // an object.
Also...
1 2 3 4 5
delete[] array; // <- array is delete[]'d here
size = arrayB.size;
for(int i=0;i<size;i++) {
array[i]=arrayB.array[i]; // <- you're writing to a delete[]'d pointer here.
// MEMORY CORRUPTION! bad bad bad
Lastly... you need to guard against self-assignment. If you do A = A; your assignment operator will not work properly.
They're declared in the main file like this:
IntArray A();
IntArray B(5);
Yes... that's exactly what I said.
IntArray A(); does not create an object named 'A'. Instead, it's a function prototype, declaring a function named 'A' that takes no parameters and returns an IntArray.
1 2 3 4 5 6
IntArray A()
{
// a function ... not an object
}
IntArray A(); // a function prototype, not an object
IntArray B(5); is different because the value of '5' makes it clear that this is passing an argument to the ctor and not defining a function parameter. Therefore 'B' is actually an object as you intended.
Right, thanks for your help. The instructor for the class wrote the main file so I assumed everything in it was correct. Oh well. I got everything figured out.