overloaded methods (error)

L

Latina

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<iostream>
#include<string.h>
#include<cctype>
using namespace std;

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

public:
friend istream& operator>>(istream& is, Date& d1);
friend ostream &operator<<(ostream &os, const Date &d1);
int operator-(Date);
bool operator>(Date);
int distinction(Date);
};

ostream &operator<<(ostream &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::eek:perator >(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::eek:perator -(Date d)
{ <--error(a function-definition is not allowed here before
'{' token) and (expected `,' or `;' before '{' token)
return distinction(d);
}

int Date::distinction(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
 
V

Victor Bazarov

Latina said:
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<iostream>
#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>>(istream& is, Date& d1);
friend ostream &operator<<(ostream &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(Date);

Similarly, declare/define this as

int distinction(const Date&) const;
};

ostream &operator<<(ostream &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::eek:perator >(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::eek:perator -(Date d)
{ <--error(a function-definition is not allowed here before
'{' token) and (expected `,' or `;' before '{' token)
return distinction(d);
}

int Date::distinction(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top