[HELP] Overloading << >> operators in a class
Apr 15, 2015 at 2:29am UTC
I am writing a program which will overload a few operators to make it so I can perform function to a class. I copied code from a very similar program in the textbook.
I am trying to code the Month.h file so that the Month.cpp file works correctly by using overloading.
I get all of these error when I run it, and don't have a clue what's going on.
The code in Month.cpp should pretty much stay the same.
Month.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <iostream>
#include <string>
#include "Month.h"
using namespace std;
int main()
{
Month m1("February" );
Month m2, m3, m4;
cout << m1.getMonthName() << endl;
cout << m1.getMonthNumber() << endl;
cout << endl;
cout << m1;
cout << endl;
m2 = ++m1;
cout << m1;
cout << m2;
cout << endl;
m3 = m1++;
cout << m1;
cout << m3;
cout << endl;
cout << "Enter the name of a month: " ;
cin >> m4;
cout << m4;
system("pause" );
return 0;
}
Month.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#ifndef MONTH_H
#define MONTH_H
#include <string>
#include <iostream>
class Month;
// Function Prototypes for Overloaded Stream Operators
ostream &operator << (ostream &, const Month &);
istream &operator >> (istream &, Month &);
class Month
{
private :
string monthName;
int monthNumber;
public :
Month()
{
monthName = "January" ;
monthNumber = 1;
}
Month(string x)
{x = monthName;}
//void setMonthName(string name)
// { monthName = name; }
//void setMonthNumber(int num)
// { monthNumber = num; }
string getMonthName() const
{ return monthName; }
int getMonthNumber() const
{
if (monthName == "January" )
monthNumber = 1;
else if (monthName == "February" )
monthNumber = 2;
else if (monthName == "March" )
monthNumber = 3;
else if (monthName == "April" )
monthNumber = 4;
else if (monthName == "May" )
monthNumber = 5;
else if (monthName == "June" )
monthNumber = 6;
else if (monthName == "July" )
monthNumber = 7;
else if (monthName == "August" )
monthNumber = 8;
else if (monthName == "September" )
monthNumber = 9;
else if (monthName == "November" )
monthNumber = 10;
else if (monthName == "December" )
monthNumber = 11;
else if (monthName == "October" )
monthNumber = 12;
else
{
cout << "/t Invalid Month!" ;
monthNumber = -1;
}
return monthNumber;
}
/////////////////////////
// Overloaded operators//
/////////////////////////
// Overloaded operator functions
Month operator + (const Month &); // Overloaded +
Month operator ++ (); // Prefix ++
Month operator ++ (int ); // Postfix ++
bool operator > (const Month &); // Overloaded >
bool operator < (const Month &); // Overloaded <
bool operator == (const Month &); // Overloaded ==
friend ostream &operator << (ostream &, const Month &);
friend istream &operator >> (istream &, Month &);
};
#endif
Last edited on Apr 15, 2015 at 2:45am UTC
Apr 15, 2015 at 2:36am UTC
The errors are there because you have an incomplete type that you are trying to use. You have not defined any of the other operations that can be done on a Month object and yet you are trying to make use of those operations in the main.
Also why did you define the name and number to be static? Static is not the same as const, if that's what you are trying to do
Apr 15, 2015 at 2:46am UTC
Fixed the static, thank you. I don't understand what you mean by having an incomplete type. Could you explain more please?
Last edited on Apr 15, 2015 at 3:09am UTC
Apr 15, 2015 at 3:08am UTC
You should define them by providing method bodies for them, for example:
1 2 3
friend std::ostream &operator << (std::ostream &oss, const Month &month) {
return oss << month.monthNumber << " : " << month.monthName;
}
1 2 3
bool operator > (const Month &month) { // Overloaded >
return monthNumber > month.monthNumber;
}
Just like that for all of them
Apr 15, 2015 at 3:39am UTC
This is what I have as of now
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#ifndef MONTH_H
#define MONTH_H
#include <string>
#include <iostream>
class Month;
// Function Prototypes for Overloaded Stream Operators
ostream &operator << (ostream &, const Month &);
istream &operator >> (istream &, Month &);
class Month
{
private :
string monthName;
int monthNumber;
public :
Month()
{
monthName = "January" ;
monthNumber = 1;
}
Month(string x)
{
monthName = x;
}
//void setMonthName(string name)
// { monthName = name; }
//void setMonthNumber(int num)
// { monthNumber = num; }
string getMonthName() const
{ return monthName; }
int getMonthNumber() const
{
int monthNumber;
if (monthName == "January" )
monthNumber = 1;
else if (monthName == "February" )
monthNumber = 2;
else if (monthName == "March" )
monthNumber = 3;
else if (monthName == "April" )
monthNumber = 4;
else if (monthName == "May" )
monthNumber = 5;
else if (monthName == "June" )
monthNumber = 6;
else if (monthName == "July" )
monthNumber = 7;
else if (monthName == "August" )
monthNumber = 8;
else if (monthName == "September" )
monthNumber = 9;
else if (monthName == "November" )
monthNumber = 10;
else if (monthName == "December" )
monthNumber = 11;
else if (monthName == "October" )
monthNumber = 12;
else
{
cout << "/t Invalid Month!" ;
monthNumber = -1;
}
return monthNumber;
}
// Overloaded operator functions
Month operator + (const Month &month); // Overloaded +
Month operator ++ (); // Prefix ++
Month operator ++ (int ); // Postfix ++
bool operator > (const Month &month) // Overloaded >
{
return monthNumber > month.monthNumber;
}
bool operator < (const Month &month) // Overloaded <
{
return monthNumber < month.monthNumber;
}
bool operator == (const Month &month) // Overloaded ==
{
return monthNumber == month.monthNumber;
}
friend ostream &operator << (ostream &oss, const Month &month)
{
return oss << month.monthNumber << " : " << month.monthName;
}
friend istream &operator >> (istream &iss, Month &month)
{
return iss >> month.monthNumber << " : " << month.monthName;
}
};
#endif
Thank you for your patience.
Apr 15, 2015 at 3:48am UTC
1 2 3
friend istream &operator >> (istream &iss, Month &month) {
return iss >> month.monthNumber >> month.monthName;
}
I have provided you with enough information that you should know what to do for the rest. I'm not sure how you would implement
Month + Month , but the others should be easy enough to do. You also don't need lines 6-10, unless you have a good reason for having them there
Apr 15, 2015 at 9:32am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
int getMonthNumber() const
{
if (monthName == "January" )
monthNumber = 1;
else if (monthName == "February" )
monthNumber = 2;
else if (monthName == "March" )
monthNumber = 3;
else if (monthName == "April" )
monthNumber = 4;
else if (monthName == "May" )
monthNumber = 5;
else if (monthName == "June" )
monthNumber = 6;
else if (monthName == "July" )
monthNumber = 7;
else if (monthName == "August" )
monthNumber = 8;
else if (monthName == "September" )
monthNumber = 9;
else if (monthName == "November" )
monthNumber = 10;
else if (monthName == "December" )
monthNumber = 11;
else if (monthName == "October" )
monthNumber = 12;
else
{
cout << "/t Invalid Month!" ;
monthNumber = -1;
}
return monthNumber;
}
you cannot modify any class member inside a const function member of the class.
where is the definition of your operator overloading functions?
Topic archived. No new replies allowed.