Overloading copy operator in custom class

Hi guys,
that’s the first time I overload an operator and I’m struggling getting this to work.
I have a custom class and I need to perform some operations if two objects of the same class are summed together. I tried this way:

MyClass operator+(const MyClass b)
{
   return myFunction(*this, b);
}

But when I try to sum two objects, I get the error message:

Object of type ‘MyClass’ cannot be assigned because its copy assignment operator is implicitly deleted

My custom class does NOT have the NON_COPYABLE macro enabled.

Thanks!

The JUCE_DECLARE_NON_COPYABLE macro would make the copy assignment operator of the class explicitly deleted, but here the compiler error says “implicitly deleted”.

There are several reasons for a copy assignment operator to be implicitly deleted. You can read more about that topic on this page: https://en.cppreference.com/w/cpp/language/copy_assignment, in the " Deleted implicitly-declared copy assignment operator" section.

I hope this helps!

1 Like

Thanks. I found out that having constant global variables makes the copy assignment implicitly deleted.

There are multiple copy actions going on btw. By supplying MyClass by-value rather than by-reference, the b is also copied. I think you accidentally missed the reference operator here?

MyClass operator+(const MyClass& b)
3 Likes