use of deleted function

Hi,
I have defined two constructors, one default and one with init parameters:
1
2
    Complex();
    Complex(double &x, double &iy) { m_x = x; m_iy = iy;}


Furthermore I have defined these operators:
1
2
3
4
5
    Complex& operator=(Complex& other);
    Complex& operator=(Complex&& other);
    Complex& operator=(const Complex& other);
    Complex& operator=(const Complex&& other);
    Complex& operator=(const Complex other);

But when I use the second constructor, as in here
 
return Complex(x,iy);

This is marked as "error: use of deleted function 'constexpr Complex::Complex(const Complex&)'
Complex Complex::squared(Complex &c) {return c.squared(); }"

I have tried to define
1
2
3
4
5
    constexpr Complex& operator=(const Complex &other) const {
        m_x = other.x();
        m_iy = other.iy();
        return *this;
    }

But this causes the following errors:
1st line: "no return statement in constexpr function"
2nd/3rd line: "cannot assign to non-static data member within const member function 'operator='"
4th line: "binding value of type 'const Complex' to reference to type 'Complex' drops 'const' qualifier"

I obviously haven't sufficiently understood the concept of contexpr (and others).

Any idea how to satisfy the compiler?

^
Hm. Seems that defining
1
2
3
4
5
    Complex(const Complex &c)
        {
             m_x = c.x();
             m_iy = c.iy();
        }

and - to avoid ambiguity - limiting my operator= definitions to one
 
Complex& operator=(Complex& other);

might have done the trick.
The ambiguity exists because you've added a copy constructor accepting an argument by-value. You don't need that one if you're trying to overload based on value category.

But taking a step back, there are a number of suspicious things about the code posted here:
- The class template std::complex already exists. Why define a new one?
- It's very rare that overloads for rvalue reference to const make sense outside of highly generic code.
- One should not typically define copy and move operations at all for a type that doesn't appear to own any resource. This is the Rule of Zero:
https://en.cppreference.com/w/cpp/language/rule_of_three
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c20-if-you-can-avoid-defining-default-operations-do
- There's no purpose to move objects of type double.
Last edited on
Topic archived. No new replies allowed.