1

I have overloaded the "+" operator so when I make class1 + class1 = class2 . The only Problem is when I pass it to another object.

The "+" operator is already overloaded so when you make class2 + class1, it adds the class1 object to the class2 object.

Here is the example:

 #include<iostream.h>

class Figura {
    public: 
        int x, y, poz;
        int tip; //1 = punct ; 2 = dreapta; 3 = dreptunghi
        Figura() { };
        Figura(const Figura&) { };
};

class Grup {
    private:
        int nr_elemente;
        Figura **figuri;
    public:
        int i;
        Grup(int nr_el) {
            nr_elemente = nr_el;
            figuri = new Figura*[nr_elemente];
            i = 0;
        }
        Grup() {
            nr_elemente = 100;
            figuri = new Figura*[nr_elemente];
            i = 0;
        }

        ~Grup() { /*delete[] figuri;*/ };

        Grup(const Grup&) { };

        Grup& operator=(const Grup&) {
            return *this;
        }

        int _nr_elemente() {
            return i;
        }

        void afiseaza_elemente() {
            for(int j = 0; j < i; j++)
                cout<<"Figura nr:"<<j<<" tip :"<<figuri[j]->tip<<" x:"<<figuri[j]->x<<" y:"<<figuri[j]->y<<" poz:"<<figuri[j]->poz<<endl;
        }
    friend Grup operator+(const Figura& fig1, const Figura& fig2) {
        Grup grt;
        grt + fig1;
        grt + fig2;
        cout<<grt.i<<endl; // current number of elements begining with 0, so it should print 1 (fig1 and fig2)
        grt.afiseaza_elemente(); // prints the elements attributes
        return grt;
    };

    friend Grup operator+(const Grup& gr1, const Grup& gr2) {};
    void operator+(const Figura& fig);

};

void Grup::operator+(const Figura& fig) {
    Grup::figuri[Grup::i] = new Figura;
    Grup::figuri[Grup::i]->tip = fig.tip;
    Grup::figuri[Grup::i]->poz = fig.poz;
    if(fig.tip == 2) {
        Grup::figuri[Grup::i]->x = fig.x;
        Grup::figuri[Grup::i]->y = 0;
    } else if(fig.tip == 3) {
        Grup::figuri[Grup::i]->x = fig.x;
        Grup::figuri[Grup::i]->y = fig.y;
    }
    else {
        Grup::figuri[Grup::i]->x = 0;
        Grup::figuri[Grup::i]->y = 0;
    }
    Grup::i++;
}

class Punct : public Figura
{
    public: 
        Punct(int poz) {
            Punct::tip = 1;
            Punct::poz = poz;
        }
};

class Segment : public Figura
{
    public:
        Segment(int poz, int x) {
            Segment::tip = 2;
            Segment::poz = poz;
            Segment::x = x;
        }
};

class Dreptunghi : public Figura
{
    public:
        Dreptunghi(int poz, int x, int y) {
            Dreptunghi::tip = 3;
            Dreptunghi::poz = poz;
            Dreptunghi::x = x;
            Dreptunghi::y = y;
        }
};

void main(void) {

    Grup gr(10);
    Punct pct(1);
    Segment sgm(3, 5);

    gr + pct;
    gr + sgm;

    Grup gr2 = pct + sgm;
    cout<<"--------------------------"<<endl;
    cout<<gr2.i<<endl; // prints a weird number
    gr2.afiseaza_elemente(); // prints the elemenets atributes, but because gr2.i is negative shows nothing

}
BebliucGeorge
  • 751
  • 2
  • 8
  • 15
  • 1
    What's the problem? What is the expected output? What is the actual output? What is the return type of `operator+(const Group&, const Figure&)`? [SSCCE](http://sscce.org)! – ildjarn Apr 23 '12 at 22:35
  • 2
    You seem to be returning `grt`, which is undefined. Perhaps you meant to return `temporary`? Also, based on your previous question, I suspect that you violate the [Rule of Three](http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)) – Robᵩ Apr 23 '12 at 22:37
  • The expected output is to show a number defined in the constructor of the Group class, lets say 1. When i pass the object with return as : another_group = fig1 + fig2, the value is lost and i get something like -80323423 instead. – BebliucGeorge Apr 23 '12 at 22:37
  • @Bdesign : And how is anyone supposed to diagnose the issue without proper, compilable code? It could be a problem in `Group`'s copy constructor, or in `operator+(const Group&, const Figure&)` -- who knows? – ildjarn Apr 23 '12 at 22:38
  • @Bdesign - Now you are missing `main`. Please create a **short** (less than 20 lines), **complete** (we are able to compile it without changing anything), sample program and paste it into your question. For more information, see http://sscce.org/. – Robᵩ Apr 23 '12 at 22:43
  • Ok, now I have edited the code so it is compilable. The data above the line "-----------" is what I should get and what is beneath it is what I get after trying to accesed the new object. – BebliucGeorge Apr 23 '12 at 22:52
  • `Grup(const Grup&) { };` is the culprit -- you need to actually implement your copy constructor (and copy assignment operator). – ildjarn Apr 23 '12 at 22:54
  • Yes, that was it. I updated it with `Grup(const Grup& fig) { nr_elemente = fig.nr_elemente; i = fig.i; figuri = fig.figuri; };` and now it works. Please make your comment an answer so I can mark it as fixed. – BebliucGeorge Apr 23 '12 at 23:02
  • @Bdesign : That only _appears_ to work because your destructor isn't implemented either. If you implement the destructor, you'll get memory corruption at runtime. Read this thread: [What is The Rule of Three?](http://stackoverflow.com/q/4172722/636019) – ildjarn Apr 23 '12 at 23:23

0 Answers0