-1
 const Polynomial operator +(const Polynomial lhs, const Polynomial rhs){
        //lhs is 1
        Polynomial result(lhs);

        result+=rhs;
        //when I add rhs to my result it also increments my lhs and lhs is now 3
        cout<<"item: "<<rhs<<endl;
        return result;
    }

        int size;
    Monomial *polynome; // my polynome is an array of monomials
                            //p[0]= 2, p[1]=3x etc.

I am using default copy constructor(i didnt define one) maybe thats the problem since I have a pointer to array I dont know. I rather not to share my += operator unles necessary since it seem like its working fine but its terribly implemented it would take 30 minutes to explain whats going on. This is driving me crazy, any solutions?

EDIT: defining a deep copy constructor solved my problem. Thanks a lot for the tips!

user2648701
  • 39
  • 1
  • 2
  • 5
  • 3
    How are `operator+=` and copy-constructor implemented? – Oliver Charlesworth Aug 15 '13 at 18:01
  • please post your definition for Polynomial – CS Pei Aug 15 '13 at 18:02
  • -1 for the lack of information. Without providing the definitions of the copy constructor and `operator+=` it is impossible to answer the question. --BTW, it really looks as if your copy constructor is wrong and using reference semantics rather than value semantics. – David Rodríguez - dribeas Aug 15 '13 at 18:10
  • Sorry for lack of information, I am completely new to programming so I dont know what information is relevant. Editing my post to add more information – user2648701 Aug 15 '13 at 18:15
  • 1
    Almost certainly, you're messing around with manual memory management and dumb pointers, and forgetting the [Rule of Three](http://stackoverflow.com/questions/4172722). But without seeing what `Polynomial` contains and how its copy semantics are defined, that's just a guess. – Mike Seymour Aug 15 '13 at 18:16
  • In addition to not following the Rule of Three, why are you not implementing the `operator+` by simply `return lhs.data + rhs.data`? – Zac Howland Aug 15 '13 at 18:27
  • @ZacHowland: Because presumably the return type needs to be `Polynomial`. – Oliver Charlesworth Aug 15 '13 at 21:58
  • @Oli: You were taking the question much to literally. The point I was getting at is simply that typically you implement += in terms of +, not the other way around. – Zac Howland Aug 16 '13 at 13:31

1 Answers1

0

I'm going to hazard a wild guess that your class looks something vaguely like this:

// DO NOT write code like this. The default copy semantics are incorrect.
struct Polynomial {
    Polynomial(size_t order) : coefs(new double[order+1]) {}
    double * coefs;
};

attempting to manage dynamic memory with a dumb pointer. Unless you write your own copy constructor and assignment operator to correctly copy the dynamic array, copying this will simply copy the pointer, leaving both pointing to the same array.

The simplest solution is to use the standard vector container, which has the copy semantics that you'd expect:

// Better: the default copy semantics correctly copy the vector
struct Polynomial {
    Polynomial(size_t order) : coefs(order+1) {}
    std::vector<double> coefs;
};
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644