overloaded methods (error)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Latina

    #1

    overloaded methods (error)

    Hi, I am doing a program using overloded operators but I am getting
    some error,
    Can some one help please.
    Here is my code:

    #include<iostre am>
    #include<string .h>
    #include<cctype >
    using namespace std;

    class Date
    {
    private:
    int mon;
    int day;
    int year;
    string m;

    public:
    friend istream& operator>>(istr eam& is, Date& d1);
    friend ostream &operator<<(ost ream &os, const Date &d1);
    int operator-(Date);
    bool operator>(Date) ;
    int distinction(Dat e);
    };

    ostream &operator<<(ost ream &out, const Date &d)
    { <--error(a function-definition is not allowed here
    before '{' token) and (expected `,' or `;' before '{' token)
    out<<d.mon<<"/"<<d.day<<"/"<<d.year;
    return out;
    }

    bool Date::operator >(Date d)
    { <--error(a function-definition is not allowed here before
    '{' token) and (expected `,' or `;' before '{' token)
    int diff = distinction(d);
    if(diff < 0)
    return true;
    else
    return false;
    }

    int Date::operator -(Date d)
    { <--error(a function-definition is not allowed here before
    '{' token) and (expected `,' or `;' before '{' token)
    return distinction(d);
    }

    int Date::distincti on(Date t)
    { <--error(a function-definition is not allowed here before
    '{' token) and (expected `,' or `;' before '{' token)
    int total = 0;

    total = (year - t.year) * 365;
    total += (month - t.month) * 30.5;
    total += (day - t.day);
    total += (year - t.year) / 4;

    if(total<0)
    total *= -1;
    return total;
    }

    Thanks
  • Victor Bazarov

    #2
    Re: overloaded methods (error)

    Latina wrote:
    Hi, I am doing a program using overloded operators but I am getting
    some error,
    Can some one help please.
    Here is my code:
    >
    #include<iostre am>
    #include<string .h>
    I believe this is supposed to be without the ".h"...
    #include<cctype >
    using namespace std;
    >
    class Date
    {
    private:
    int mon;
    int day;
    int year;
    string m;
    >
    public:
    friend istream& operator>>(istr eam& is, Date& d1);
    friend ostream &operator<<(ost ream &os, const Date &d1);
    int operator-(Date);
    Declare/define it as

    int operator-(const Date&) const;
    bool operator>(Date) ;
    Same here, declare/define it as

    bool operator>(const Date&) const;
    int distinction(Dat e);
    Similarly, declare/define this as

    int distinction(con st Date&) const;
    };
    >
    ostream &operator<<(ost ream &out, const Date &d)
    { <--error(a function-definition is not allowed here
    before '{' token) and (expected `,' or `;' before '{' token)
    It seems that you've either misplaced a curly brace or a semicolon.
    out<<d.mon<<"/"<<d.day<<"/"<<d.year;
    return out;
    }
    >
    bool Date::operator >(Date d)
    { <--error(a function-definition is not allowed here before
    '{' token) and (expected `,' or `;' before '{' token)
    int diff = distinction(d);
    if(diff < 0)
    return true;
    else
    return false;
    }
    >
    int Date::operator -(Date d)
    { <--error(a function-definition is not allowed here before
    '{' token) and (expected `,' or `;' before '{' token)
    return distinction(d);
    }
    >
    int Date::distincti on(Date t)
    { <--error(a function-definition is not allowed here before
    '{' token) and (expected `,' or `;' before '{' token)
    int total = 0;
    >
    total = (year - t.year) * 365;
    total += (month - t.month) * 30.5;
    total += (day - t.day);
    total += (year - t.year) / 4;
    >
    if(total<0)
    total *= -1;
    return total;
    }
    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Latina

      #3
      Re: overloaded methods (error)

      I got it thanks ^_^

      Comment

      Working...