5 Replies - 288 Views - Last Post: 05 October 2012 - 07:29 PM Rate Topic: -----

#1 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 188
  • Joined: 25-March 10

Compiler can't do relational operations, is the overloading wrong?

Posted 05 October 2012 - 03:58 PM

The purpose of the program is to let the user enter a day, and then it will display the corresponding month.

The problem occurs on line 79 and 85 with the > relational operator. On line 79 I'll have to use <=, but for now I'd like to take things one at a time.

The error reads: no operator "<" matches these operands.

#include <iostream>
#include <string>
using namespace std;

class DayYear
{
private:
	int day;

	static const string MonthDays[];
	static const string Months[];

public:
	DayYear()
	{	day = 0;	}

	DayYear(int)
	{	;	}

	void setDay(int)
	{;}

	void print()
	{;}

	friend istream& operator>>(istream &, DayYear &);

	//How do i define the following 2 operators headers, out side of the class, instead of inline?
	bool operator < (DayYear &c)
	{
		if (day < c.day)
			return true;
		else 
			return false;	
	}

	bool operator > (DayYear &c)
	{
		if (day > c.day)
			return true;
		else 
			return false;	
	}

	bool operator == (DayYear &c)	
	{
		if (day == c.day)
			return true;
		else 
			return false;	
	}




};

const int MonthDays[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};

const string MonthName[] = {"January", "February", "March", "April", 
							"May", "June", "July", "August",
							"September", "October", "November", "December"};

DayYear::DayYear(int d)
{
	setDay(d);
}

void DayYear::setDay(int d)
{
	day = d;
}

void DayYear::print()
{
	int i = 0;
	do
	{ 
		if (day < MonthDays[i])		//should be <=, but I'm doing thing 1 at time for now.
		{
			cout << "\nThe month of the day is: " << MonthName[i] << ".\n";
		}
		else
			i++;
	} while (day > MonthDays[i]);
}

istream & operator>>(istream &input, DayYear &dy)
{
	int d;

	do {
		cout << "Enter number of days: ";
		input >> d;
	} while (d < 0 || d > 365);

	dy.setDay(d);

	return input;
}

int main()
{
	DayYear call1;
	
	cin >> call1;

	call1.print();
	
	system("pause");
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Compiler can't do relational operations, is the overloading wrong?

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,563
  • Joined: 25-December 09

Re: Compiler can't do relational operations, is the overloading wrong?

Posted 05 October 2012 - 04:20 PM

I don't get the error you seem to be reporting. But I do get these errors:

Quote

main.cpp|64|error: redefinition of ‘DayYear::DayYear(int)’|
main.cpp|17|error: ‘DayYear::DayYear(int)’ previously defined here|
main.cpp|69|error: redefinition of ‘void DayYear::setDay(int)’|
main.cpp|20|error: ‘void DayYear::setDay(int)’ previously defined here|
main.cpp|74|error: redefinition of ‘void DayYear::print()’|
main.cpp|23|error: ‘void DayYear::print()’ previously defined here|
||=== Build finished: 6 errors, 0 warnings ===|


Edit: after fixing those errors I got these errors:
Spoiler

You have overloaded your operator< to compare two of your classes, not your class and a int.


Jim

This post has been edited by jimblumberg: 05 October 2012 - 04:27 PM

Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,332
  • Joined: 08-August 08

Re: Compiler can't do relational operations, is the overloading wrong?

Posted 05 October 2012 - 04:37 PM

What is line 10 doing?
Was This Post Helpful? 2
  • +
  • -

#4 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 188
  • Joined: 25-March 10

Re: Compiler can't do relational operations, is the overloading wrong?

Posted 05 October 2012 - 06:04 PM

Thanks, line 10 was indeed the main culprit. I'd still appreciate it, if someone told me how to declare the overloaded relational operators (<,>, ==), outside of the class definition/header block.

If someone may tell me why line 10 and 11 need to be static, it would be appreciated as well. I know static are primarily indeed to keep track of how many times the function/member has been called.

This post has been edited by mgrex: 05 October 2012 - 06:13 PM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,117
  • Joined: 05-May 12

Re: Compiler can't do relational operations, is the overloading wrong?

Posted 05 October 2012 - 06:52 PM

Those are static because you don't really need each instance of your class having their own separate copy of those arrays. They are intended to be used for lookup purposes only so why have multiple copies when one will suffice?
Was This Post Helpful? 1
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,631
  • Joined: 19-February 09

Re: Compiler can't do relational operations, is the overloading wrong?

Posted 05 October 2012 - 07:29 PM

Declaring outside of class definition/header block.

bool DayYear::operator > (DayYear &c)
{
  if (day > c.day)
    return true;
  else 
    return false;  
}




The problem here, is the while condition which tests the next month before any print out.


077	    do
078	    {
079	        if (day < MonthDays[i])      //should be <=, but I'm doing thing 1 at time for now.
080	        {
081	            cout << "\nThe month of the day is: " << MonthName[i] << ".\n";
082	        }
083	        else
084	            i++;
085	    } while (day > MonthDays[i]);


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1