9 Replies - 874 Views - Last Post: 02 March 2010 - 03:35 AM Rate Topic: -----

#1 GreenOrc  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 28-February 10

[SOLVED] 2 errors! Need some help with overloaded operation==

Posted 01 March 2010 - 10:25 PM

I'm trying to compare 2 objects D1 and D2. Get 2 errors. Cannot figure out what's wrong. I'm kind of new to overloaded operations.

Header file: Date.h

//FILE: Date.h
//CLASS PROVIDED: Date
//CONSTRUCTORS for the Date class:
//Date();
//Precondition: month = 1, day = 1; year = 1900.
//Date(int, int, int);
//Precondition: month = 1, day = 1; year = 1900.
//Postcondition: the date is changed above or equal 01/01/1900.
//MODIFICATION MEMBER FUNCTIONS for the Date class:
//CONSTANT MEMBER FUNCTIONS for the Date class:
//NONMEMBER FUNCTIONS:
//bool isValidDate (int, int, int);
//bool isLeapYear(int);
//VALUE SEMANTICS for the Date class:
#ifndef DATE_H //infdef = If Not Defined
#define DATE_H
namespace DATE
{
	class Date
	{
	private:
		unsigned int theMonth;
		unsigned int theDay;
		unsigned int theYear;

	public:
		//CONSTRUCTORS:
		Date();
		Date(int, int, int);
	
		void setMonth(int month);
		int getMonth ();
		void setDay (int day);
		int getDay ();
		void setYear (int year);
		int getYear ();

		//MEMBER FUNCTIONS:
		string showDate (int month, int day, int year);

		//DESSTRUCTOR:
		~Date();
	};
	//NONMEMBER FUNCTIONS:
	bool isValidDate (int, int, int);
	bool isLeapYear(int);
	bool operator == (const Date& D1, const Date& D2);
}
#endif



Implementation file: Date.cpp:

#include <iostream> //Provides cout, cin, sprintf
#include <string.h> //Provides string
using namespace std;
#include "Date.h"
namespace DATE
{
	//CONSTRUCTORS:
	Date::Date ()
	{
		theMonth = 1;
		theDay = 1;
		theYear = 1900;
	}
	Date::Date(int month, int day, int year)
	{
		theMonth = month;
		theDay = day;
		theYear = year;
	}
	void Date::setMonth(int month)
    {
        theMonth = month;
    }
    int Date::getMonth()
    {
        return theMonth;
    }
    void Date::setDay(int day)
    {
         theDay = day;
    }
    int Date::getDay()
    {
        return theDay;
    }
    void Date::setYear(int year)
    {
        theYear = year;
    }
    int Date::getYear()
    {
        return theYear;
    }    
	//MEMBER FUNCTIONS:
	
	string Date::showDate (int mm, int dd, int yyyy)
	{
		char dateString [11];
		sprintf_s (dateString, "%02i/%02i/%04i", mm,dd,yyyy);
		string s = dateString;
		return s;
	}
	//DESTRUCTOR:
	Date::~Date()
	{
	}
	//NONMEMBER FUNCTIONS:
	bool isLeapYear (int yyyy)
	{
		if ((yyyy%100)==0 && (yyyy%400)==0)
		{
			cout<<"Leap year!";
			return true;
		}
		else if ((yyyy%100)!=0 && (yyyy%4)==0)
		{
			cout<<"--> Leap year!";
			return true;
		}
		else
		{
			cout<<"--> Normal year!";
			return false;
		}
	}
	bool isValidDate (int mm, int dd, int yyyy)
	{
		if ((mm == 2) && (dd <= 29) && (yyyy >= 1900))
		{
			cout<< "Valid date!";
			return true;
		}
		else if ((mm != 2) && (mm <= 12) && (dd <= 31) && (yyyy >= 1900))
		{
			cout<< "Valid date!";
			return true;
		}
		else
		{
			cout<< "Invalid date! Please re-enter the date!";
			return false;
		}
	}	
	bool operator == (const Date& D1, const Date& D2)
	{
		return (D1.getYear() == D2.getYear());
	}
}



driver file: DateDemo.cpp:

#include <iostream>
#include <string.h>
using namespace std;
#include "Date.h"
using namespace DATE;

int main ()
{	
	int mm1;
	int dd1;
	int yyyy1;
	
	do
	{	
		cout<< endl;
		cout<< "First date!\nEnter the month in mm format: ";
		cin>>mm1;
		cout<< "Enter the day in dd format: ";
		cin>>dd1;
		cout<< "Enter the year in yyyy format: ";
		cin>> yyyy1;
	}	while (isValidDate(mm1, dd1, yyyy1) == false);
	cout<< endl;
	isLeapYear (yyyy1);
	cout<< endl;

	int mm2;
	int dd2;
	int yyyy2;

	do
	{	
		cout<< endl;
		cout<< "Second date!\nEnter the month in mm format: ";
		cin>>mm2;
		cout<< "Enter the day in dd format: ";
		cin>>dd2;
		cout<< "Enter the year in yyyy format: ";
		cin>> yyyy2;
	}	while (isValidDate(mm2, dd2, yyyy2) == false);
	cout<< endl;
	isLeapYear (yyyy2);
	cout<< endl<<endl;

	Date D1 (mm1, dd1, yyyy1);
	Date D2 (mm2, dd2, yyyy2);

	if (D1 == D2)
	{
		cout << "Those points are equal." << endl;
	}
	
	string s = D1.showDate (mm1, dd1, yyyy1);
	string s2 = D2.showDate (mm2, dd2, yyyy2);

	cout<< "D1 = "<<s.c_str () << endl;
	cout<< "D2 = "<<s2.c_str () << endl;
		
	system ("pause");
	return 0;
}


This post has been edited by GreenOrc: 02 March 2010 - 03:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [SOLVED] 2 errors! Need some help with overloaded operation==

#2 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: [SOLVED] 2 errors! Need some help with overloaded operation==

Posted 01 March 2010 - 10:26 PM

Mind giving us the compilation output? Makes error checking so much easier. Thanks =)
Was This Post Helpful? 1
  • +
  • -

#3 GreenOrc  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 28-February 10

Re: [SOLVED] 2 errors! Need some help with overloaded operation==

Posted 01 March 2010 - 10:29 PM

View Postbodom658, on 01 March 2010 - 09:26 PM, said:

Mind giving us the compilation output? Makes error checking so much easier. Thanks =)


Here the errors:
1>------ Build started: Project: DateDemo, Configuration: Debug Win32 ------
1>Compiling...
1>Date.cpp
1>c:\documents and settings\0934454\my documents\visual studio 2008\projects\datedemo\datedemo\date.cpp(126) : error C2662: 'DATE::Date::getYear' : cannot convert 'this' pointer from 'const DATE::Date' to 'DATE::Date &'
1>        Conversion loses qualifiers
1>c:\documents and settings\0934454\my documents\visual studio 2008\projects\datedemo\datedemo\date.cpp(126) : error C2662: 'DATE::Date::getYear' : cannot convert 'this' pointer from 'const DATE::Date' to 'DATE::Date &'
1>        Conversion loses qualifiers
1>Build log was saved at "file://c:\Documents and Settings\0934454\My Documents\Visual Studio 2008\Projects\DateDemo\DateDemo\Debug\BuildLog.htm"
1>DateDemo - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Was This Post Helpful? 0
  • +
  • -

#4 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: [SOLVED] 2 errors! Need some help with overloaded operation==

Posted 01 March 2010 - 10:47 PM

Define the operator within the class, not the namespace.
Was This Post Helpful? 0
  • +
  • -

#5 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: [SOLVED] 2 errors! Need some help with overloaded operation==

Posted 01 March 2010 - 10:53 PM

The errors states that you have requested a const reference to a date but you are giving a non-const reference. I would suggest you get rid of the const from the operator == prototype in the header and from the function definition in the code file.
Was This Post Helpful? 2
  • +
  • -

#6 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: [SOLVED] 2 errors! Need some help with overloaded operation==

Posted 01 March 2010 - 10:55 PM

While that would work, since the left hand side is a member of the class, I don't see why to not define it within the class.

The issue I see is that the "this" pointer in a non-const function is... not const. Keep the right hand side as const, but not the left (or you could declare the operator const).

This post has been edited by PlasticineGuy: 01 March 2010 - 10:56 PM

Was This Post Helpful? 1
  • +
  • -

#7 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: [SOLVED] 2 errors! Need some help with overloaded operation==

Posted 01 March 2010 - 11:07 PM

@GreenOrc in order to get rid of both these errors change the

bool operator == (const Date& D1, const Date& D2); 



to

bool operator == (Date& D1, Date& D2); 



in the header file and then

        bool operator == (const Date& D1, const Date& D2) 
        { 
                return (D1.getYear() == D2.getYear()); 
        } 



to

        bool operator == (Date& D1, Date& D2) 
        { 
                return (D1.getYear() == D2.getYear()); 
        } 



and the program works fine, albeit it just checks the year at the moment.
Was This Post Helpful? 1
  • +
  • -

#8 GreenOrc  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 28-February 10

Re: [SOLVED] 2 errors! Need some help with overloaded operation==

Posted 02 March 2010 - 02:21 AM

I am very appreciated all your replies. They are very helpful.You guys are right about the const.

But my teacher requires me to put the operator == into the member function. I change my code as follow, and there are now 3 errors:

Errors:
1>c:\users\davy uong\documents\visual studio 2010\projects\datedemo\datedemo\date.cpp(47): error C2143: syntax error : missing ')' before '.'
1>c:\users\davy uong\documents\visual studio 2010\projects\datedemo\datedemo\date.cpp(47): error C2059: syntax error : '.'
1>c:\users\davy uong\documents\visual studio 2010\projects\datedemo\datedemo\date.cpp(47): error C2059: syntax error : ')'


Date.h

//FILE: Date.h
//CLASS PROVIDED: Date
//CONSTRUCTORS for the Date class:
//Date();
//Precondition: month = 1, day = 1; year = 1900.
//Date(int, int, int);
//Precondition: month = 1, day = 1; year = 1900.
//Postcondition: the date is changed above or equal 01/01/1900.
//MODIFICATION MEMBER FUNCTIONS for the Date class:
//CONSTANT MEMBER FUNCTIONS for the Date class:
//NONMEMBER FUNCTIONS:
//bool isValidDate (int, int, int);
//bool isLeapYear(int);
//VALUE SEMANTICS for the Date class:
#ifndef DATE_H //infdef = If Not Defined
#define DATE_H
namespace DATE
{
	class Date
	{
	private:
		unsigned int theMonth;
		unsigned int theDay;
		unsigned int theYear;

	public:
		//CONSTRUCTORS:
		Date();
		Date(int, int, int);
	
		void setMonth(int month);
		int getMonth () const;
		void setDay (int day);
		int getDay () const;
		void setYear (int year);
		int getYear () const;

		//MEMBER FUNCTIONS:
		string showDate (int month, int day, int year);
		bool operator == (const Date& D2) const;

		//DESSTRUCTOR:
		~Date();
	};
	//NONMEMBER FUNCTIONS:
	bool isValidDate (int, int, int);
	bool isLeapYear(int);
	//bool operator == (const Date& D1, const Date& D2);
	//bool operator < (const Date& D1, const Date& D2);
	//bool operator > (const Date& D1, const Date& D2);
}
#endif



Date.cpp

#include <iostream> //Provides cout, cin, sprintf
#include <string.h> //Provides string
using namespace std;
#include "Date.h"
namespace DATE
{
	//CONSTRUCTORS:
	Date::Date ()
	{
		theMonth = 1;
		theDay = 1;
		theYear = 1900;
	}
	Date::Date(int month, int day, int year)
	{
		theMonth = month;
		theDay = day;
		theYear = year;
	}
	void Date::setMonth(int month)
    {
        theMonth = month;
    }
    int Date::getMonth() const
    {
        return theMonth;
    }
    void Date::setDay(int day)
    {
         theDay = day;
    }
    int Date::getDay() const
    {
        return theDay;
    }
    void Date::setYear(int year)
    {
        theYear = year;
    }
    int Date::getYear() const
    {
        return theYear;
    }    
	//MEMBER FUNCTIONS:
	bool Date::operator == (const Date& D2) const
	{
		return (Date.getYear() == D2.getYear()) &&
			(Date.getMonth() == D2.getMonth()) &&
			(Date.getDay() == D2.getDay());
	}

	string Date::showDate (int mm, int dd, int yyyy)
	{
		char dateString [11];
		sprintf_s (dateString, "%02i/%02i/%04i", mm,dd,yyyy);
		string s = dateString;
		return s;
	}
	//DESTRUCTOR:
	Date::~Date()
	{
	}
	//NONMEMBER FUNCTIONS:
	bool isLeapYear (int yyyy)
	{
		if ((yyyy%100)==0 && (yyyy%400)==0)
		{
			cout<<"Leap year!";
			return true;
		}
		else if ((yyyy%100)!=0 && (yyyy%4)==0)
		{
			cout<<"--> Leap year!";
			return true;
		}
		else
		{
			cout<<"--> Normal year!";
			return false;
		}
	}
	bool isValidDate (int mm, int dd, int yyyy)
	{
		int daysInMonth [12] = {31,0,31,30,31,30,31,31,30,31,30,31};
		if ((mm == 2) && (dd <= 28) || ((mm == 2) && (dd == 29) && isLeapYear(yyyy)) && (yyyy >= 1900))
		{
			cout<< "Valid date!";
			return true;
		}
		else if ((mm != 2) && (mm <= 12) && (dd <= daysInMonth[mm-1]) && (yyyy >= 1900))
		{
			cout<< "Valid date!";
			return true;
		}
		else
		{
			cout<< "Invalid date! Please re-enter the date!";
			return false;
		}
	}	
	/*bool operator == (const Date& D1, const Date& D2)
	{
		return (D1.getYear() == D2.getYear()) &&
			(D1.getMonth() == D2.getMonth()) &&
			(D1.getDay() == D2.getDay());
	}*/	
}



DateDemo.cpp

#include <iostream>
#include <string.h>
using namespace std;
#include "Date.h"
using namespace DATE;

int main ()
{	
	int mm1;
	int dd1;
	int yyyy1;
	
	do
	{	
		cout<< endl;
		cout<< "First date!\nEnter the month in mm format: ";
		cin>>mm1;
		cout<< "Enter the day in dd format: ";
		cin>>dd1;
		cout<< "Enter the year in yyyy format: ";
		cin>> yyyy1;
	}	while (isValidDate(mm1, dd1, yyyy1) == false);
	cout<< endl;
	isLeapYear (yyyy1);
	cout<< endl;

	int mm2;
	int dd2;
	int yyyy2;

	do
	{	
		cout<< endl;
		cout<< "Second date!\nEnter the month in mm format: ";
		cin>>mm2;
		cout<< "Enter the day in dd format: ";
		cin>>dd2;
		cout<< "Enter the year in yyyy format: ";
		cin>> yyyy2;
	}	while (isValidDate(mm2, dd2, yyyy2) == false);
	cout<< endl;
	isLeapYear (yyyy2);
	cout<< endl<<endl;

	Date D1 (mm1, dd1, yyyy1);
	Date D2 (mm2, dd2, yyyy2);

	if (D1.operator == (D2))
	{
		cout << "D1 < D2  is " << "FALSE" << endl;
		cout << "D1 <= D2 is " << "TRUE" << endl;
		cout << "D1 == D2 is " << "TRUE" << endl;
		cout << "D1 >= D2 is " << "TRUE" << endl;
		cout << "D1 > D2 is " << "FALSE" << endl;
	}

	/*if (D1 == (D2))
	{
		cout << "D1 < D2  is " << "FALSE" << endl;
		cout << "D1 <= D2 is " << "TRUE" << endl;
		cout << "D1 == D2 is " << "TRUE" << endl;
		cout << "D1 >= D2 is " << "TRUE" << endl;
		cout << "D1 > D2 is " << "FALSE" << endl;
	}*/
	
	string s = D1.showDate (mm1, dd1, yyyy1);
	string s2 = D2.showDate (mm2, dd2, yyyy2);

	cout<< "D1 = "<<s.c_str () << endl;
	cout<< "D2 = "<<s2.c_str () << endl;
		
	system ("pause");
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#9 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: [SOLVED] 2 errors! Need some help with overloaded operation==

Posted 02 March 2010 - 03:08 AM

bool Date::operator == (const Date& D2) const
        {
                return (Date.getYear() == D2.getYear()) &&
                        (Date.getMonth() == D2.getMonth()) &&
                        (Date.getDay() == D2.getDay());
        }
Since this is a member function, you can call the functions without referring to any instance.
//This
int Class::member_function(void) { return get_my_int(); }
//Not this
int Class::member_function(void) { return Class.get_my_int(); }


Also, in your main file:
if (D1.operator == (D2))
You missed the point of operator overloading -- you can simply do:
if(D1 == D2)


And don't use system("PAUSE"). Use:
cout << "Press enter to continue." << endl;
cin.ignore();
cin.get();
Or something similar.

This post has been edited by PlasticineGuy: 02 March 2010 - 03:26 AM

Was This Post Helpful? 2
  • +
  • -

#10 GreenOrc  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 28-February 10

Re: [SOLVED] 2 errors! Need some help with overloaded operation==

Posted 02 March 2010 - 03:35 AM

&PlasticineGuy
Thank you so much for helping me out! Very appreciated it. +2 for you sir (:D). You've just taught me 3 new things:

"Since this is a member function, you can call the functions without referring to any instance."
bool Date::operator == (const Date& D2) const
	{
		return (getYear() == D2.getYear()) &&
			(getMonth() == D2.getMonth()) &&
			(getDay() == D2.getDay());
	}



"You missed the point of operator overloading -- you can simply do: if(D1 == D2)"

and using:
cout << "Press enter to continue." << endl;
cin.ignore();
cin.get();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1