Fraction class

Hello everybody,
I created a class managing fractions named ZFraction.
Here is the code of the operator- function (out of the class) which is the minus operator:

1
2
3
4
5
6
ZFraction operator-(ZFraction const& a)
{
    ZFraction copie(a);
    copie*=-1; // here I don't understand
    return copie;
}


I guess -1 is converted into a ZFraction object, but I tried to write
 
copie*=(-1,2)

and the compiler says that left operand of comma operator has no effect so it multiplies the current fraction object by 2.

Any explanation is welcome.
Have you overloaded the *= operator for ZFraction? Normally a *= b; means the same as a = a * b;.
Yes, see the code below:

1
2
3
4
5
6
7
8
ZFraction& ZFraction::operator*=(const ZFraction& a)
{
    m_num*=a.m_num;
    m_den*=a.m_den;
    reduire();    //makes the fraction irreducible

    return *this;
}


It's a method of the class ZFraction.
So copie*=-1 is understood by the compiler in this way : copie=copie*(-1).

Maybe the constructor is called here. My constructor is a 3 in 1 one :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
declaration:
ZFraction(int num=0,int den=1);

definition:
ZFraction::ZFraction(int num,int den):m_num(num),m_den(den)
{
    if (m_den==0)
    {
        std::cout<<"Erreur : division par 0"<<std::endl;
        exit(EXIT_FAILURE);
    }

    signe();
    reduire();


}
Maybe the constructor is called here. My constructor is a 3 in 1 one :
Yes, it is called with num = -1 and den = 1 (default)
But why write
 
copie*=(-1,2)

doesn't work?
Because (-1, 2) is equal to 2. If you want to creeate a Fraction object, you should write Fraction(-1, 2), or you can try uniform initialisation and do copie *= {-1,2};
I've tried to put copie*={-1,2}; and the compiler complains with 2 warnings, saying :
1
2
warning: extended initializer lists only available with -std=c++11 or -std=gnu++11[enabled by default]
warning: extended initializer lists only available with -std=c++11 or -std=gnu++11[enabled by default]
So you need to turn on C++11 support. How to do that differs between different IDE, but is probaly is somewhere in compiler/build options.
Thanks. I have to check, in Settings->Compiler in compiler flags, the box :
Have g++ follow the C++11 ISO C++ language standard[-std=c++11]
Topic archived. No new replies allowed.