Thread: Brushing Up on my C++

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Brushing Up on my C++

    I was brushing up on my C++....overloading operators, that is. So I got to writing the good ol' Fraction class. However, I ran into these errors that I'm not sure how to fix.

    Code:
    class Fraction{
          public:
                 int num, den, coef; //numerator, denominator, coefficient
                 Fraction():num(1),den(1),coef(0){} //default constructor
                 Fraction(int numx, int denomx){
                      num=numx;
                      den=denomx;
                      coef=0;
                 }
                 ~Fraction(){}
                 Fraction& operator+(Fraction& frac2);
                 void print(){std::cout<<num<<"/"<<den<<"\n";}
    };
    
    Fraction& Fraction::operator+(Fraction& frac2){
              Fraction temp;
              temp.num = (this.num * frac2.den) + (this.den * frac2.num);//new numerator
              temp.den = this.den*frac2.den;
              return temp;
    }
          
    
    int main(void){
        Fraction A(1,2);
        Fraction B(2,3);
        Fraction C = A+B;
        C.print();
        std::cin.get();
        return 0;
    }
    'num' is not a type
    'den' is not a type
    request for member of non-aggregate type before '*' token
    Last edited by Krak; 09-24-2005 at 01:04 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    4

    Smile Try some of these

    - I'm not sure, this operator is "called" from an object of your class, so you can use num and den without "this":

    temp.num = (num * frac2.den) + (den * frac2.num);

    - If that doesn't work try to write this:

    temp.num = ((*this).num * frac2.den) + ((*this).den * frac2.num);

    because I think "this" is a pointer to current object.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You would use this->variable. not this.variable.
    Woop?

  4. #4
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Thank you, gentlemen.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    1
    Hi,
    I don't think that your programm runs correctly 'cause you are returning a reference to a local object.
    Code:
    __Fraction& __
    Fraction::operator+(Fraction& frac2){
              Fraction temp;
              temp.num = (this.num * frac2.den) + (this.den * frac2.num);//new numerator
              temp.den = this.den*frac2.den;
              return __temp;__
    }
    As far as I remember operator+ should return a new Fraction object and operatopr+= should return a reference to *this.

  6. #6
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by Ko_Onrad
    Hi,
    I don't think that your programm runs correctly 'cause you are returning a reference to a local object.
    Code:
    __Fraction& __
    Fraction::operator+(Fraction& frac2){
              Fraction temp;
              temp.num = (this.num * frac2.den) + (this.den * frac2.num);//new numerator
              temp.den = this.den*frac2.den;
              return __temp;__
    }
    As far as I remember operator+ should return a new Fraction object and operatopr+= should return a reference to *this.
    I actually had warnings about that. What's really so bad about that? I create 'temp' which is a Fraction....and return it, which returns a reference to it....what's so bad about that?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    __Fraction& __
    Fraction::operator+(Fraction& frac2){
    You create a local object. You return a reference to it. But by the time you go to use that reference, the object has gone out of scope! In other words, it no longer exists.

    You need to return a copy, not a reference (or pointer).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed