1

I am trying to get a self-made object into a set. I have got the following code:

primefactors.insert(MultiplyOfPrimeNumber(nextPrimeNumber.GetNumber(),1));

Where primefactors is a set and MultiplyOfPrimeNumber is an object which looks like this:

#pragma once
class MultiplyOfPrimeNumber
{
public:
    MultiplyOfPrimeNumber(void);
    MultiplyOfPrimeNumber(int ,int);
    MultiplyOfPrimeNumber(const MultiplyOfPrimeNumber&);
    int PrimeNumber() const;
    int Power() const;
    void AddPower(int);
    void ChangePrimeNumber(int);
    ~MultiplyOfPrimeNumber(void);
private:
    int primenumber;
    int power;

};

bool operator<(const MultiplyOfPrimeNumber&,const MultiplyOfPrimeNumber&);

And the implementation of the operator overload:

bool operator<(const MultiplyOfPrimeNumber& left,const MultiplyOfPrimeNumber& right)
{
    if(left.PrimeNumber()<right.PrimeNumber())
    {
        return true;
    }
    else
    {
        return false;
    }
}

This code doesn't give a compile error, but when I insert the object into the set, the two integers which my object contains become -842150451. (the maximum value of a integer?) Why does -842150451 become assigned to my integers?

Gandaro
  • 3,427
  • 1
  • 17
  • 19
Corne
  • 51
  • 2
  • 1
    You left out the implementation of nextPrimeNumber.GetNumber() and your constructors. Please provide the code or we won't be able to help you. – Pepe Feb 27 '12 at 15:03
  • please show the `primefactors`, `nextPrimeNumber` declarations and the `MultiplyOfPrimeNumber(int ,int)` implementation. – vulkanino Feb 27 '12 at 15:05
  • 1
    I note that you have provided a copy constructor and a destructor, but no assignment operator. That violates the [Rule of Three](http://stackoverflow.com/q/4172722/140719). From what you show here, neither would be needed. If there's more to the class than what you show, all three might be needed. – sbi Feb 27 '12 at 15:05
  • 1
    Hint: just do `return left.PrimeNumber() –  Feb 27 '12 at 16:36
  • Please show us, how did you implement the copy constructor? – Igor Chornous Feb 27 '12 at 16:40
  • Thanks for the reply's, turns out I forgot to implement my copy constructor. So the constructor didn't make a copy of my two integer variables. – Corne Feb 27 '12 at 19:53

0 Answers0