1

I have an error in my code, I want to display the sume of 2 objects with pointers in a class. Please help me to fix it, maybe is due to the pointers. Can you see what's wrong?

This is the error:

<source>(79): error C2280: 'Pair &Pair::operator =(const Pair &)': attempting to reference a deleted function
<source>(60): note: compiler has generated 'Pair::operator =' here
<source>(60): note: 'Pair &Pair::operator =(const Pair &)': function was implicitly deleted because 'Pair' has a user-defined move constructor
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class Pair {
    int *x,  *y;
public:
    Pair() {
        x = new int(sizeof(x));
        y = new int(sizeof(y));
        *x = 0;
        *y = 0;
    }
    Pair(int a, int b) {
        x = new int(sizeof(x));
        y = new int(sizeof(y));
        *x = a;
        *y = b;
    }
    Pair(Pair& ob) {
        x = new int(sizeof(ob.x));
        y = new int(sizeof(ob.y));
        *x = *(ob.x);
        *y = *(ob.y);
    }
    Pair(Pair&& ob) {
        x = new int(sizeof(ob.x));
        y = new int(sizeof(ob.y));
        *x = *(ob.x);
        *y = *(ob.y);
    }
    Pair(int a):Pair(a, 0) {
        
    }
    void setX(int X) {
        *x = X;
    }
    void setY(int Y) {
        *y = Y;
    }
    int* getX() {
        return x;
    }
    int* getY() {
        return y;
    }
    ~Pair() {
        delete[]x;
        delete[]y;
    }
    Pair sume(Pair ob1){
        Pair ob2;
        *(ob2.x) = *(ob1.x) + (*x);
        *(ob2.y) = *(ob1.y) + (*y);
        return ob2;
    }
    double media() {
        return (double(*x) + double(*y)) / 2;
    }
};

int main() {
    Pair ob1, ob2(5), ob3(4, 3);
    ob1.setX(6);
    ob1.setY(7);
    cout << "X= " << *(ob1.getX())<<endl;
    cout << "Y= " << *(ob1.getY())<<endl;
    cout << "Media este: " << ob1.media();

    cout << "\nX= " << *(ob2.getX()) << endl;
    cout << "Y= " << *ob2.getY() << endl;
    cout << "Media este: " << ob2.media();

    cout << "\nX= " << *(ob3.getX()) << endl;
    cout << "Y= " << *(ob3.getY()) << endl;
    cout << "Media este: " << ob3.media();

    Pair ob4,ob5,ob6;
    ob4 = ob1.sume(ob2);//here the compiler shows the error
   
    
    cout <<"\nX= "<< *(ob4.getX())<<endl;
    cout << "Y= " << *(ob4.getY())<<endl;

}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • Please [edit] your question to include a full and complete copy-paste of the build-log. Also please try to condense the code into a [mre]. And add a comment on the line in the code where you get the error. – Some programmer dude Sep 07 '22 at 08:11
  • 3
    As a hint: Why do you use e.g. `new int(sizeof(x))`? Who told you to do that? Why are you even using pointers for single `int` values? That just makes no sense. – Some programmer dude Sep 07 '22 at 08:13
  • 1
    @Someprogrammerdude I think asking for an MCVE here is a bit of overkill. The error message is given and the code posted can be copy/pasted to reproduce the *exact* issue. – Adrian Mole Sep 07 '22 at 08:17
  • Also, I guess you are confusing `new(x)` with `new[x]`. It is the latter that you need to create an array, which your use of `delete[]` in the destructor implies. But there is so much else wrong with your code, in that case. What is the intent of using `sizeof(x)` (and similar) as the size of the array? – Adrian Mole Sep 07 '22 at 08:21
  • @AdrianMole I'm not really sure the asker want to create a couple of arrays, they should clarify. – Bob__ Sep 07 '22 at 08:24
  • To fix the reported error, either add an assignment operator (which is needed for the assignment, strangely enough), or 'convert' that assignment into a copy-initializer, like this: `Pair ob4 = ob1.sume(ob2);`. – Adrian Mole Sep 07 '22 at 08:24
  • 2
    The full error message is fairly self explanatory, you've defined a move constructor so the compiler has deleted the implicit copy assignment operator. You should follow the rule of five: https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – Alan Birtles Sep 07 '22 at 08:25
  • 1
    @Bob__ I agree there are some issues with this Q (why I don't really want to post an answer). But the *specific* reported error is clear and can be addressed. – Adrian Mole Sep 07 '22 at 08:25
  • somewhat related: https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – 463035818_is_not_an_ai Sep 07 '22 at 08:38
  • 2
    I would rather recommend [the rule of *zero*](https://en.cppreference.com/w/cpp/language/rule_of_three#Rule_of_zero) which can be easily achieved by not using pointers. Or if pointers are a requirement (for some unknown reason) use a smart pointer like [`std::unique_ptr`](https://en.cppreference.com/w/cpp/memory/unique_ptr). – Some programmer dude Sep 07 '22 at 08:38
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Sep 07 '22 at 15:12

0 Answers0