Question
My code doesn't compile, Im getting /tmp/Chrono-ea8306.o: In function `operator++(Chrono::Month&)': Chrono.cpp:(.text+0x1c0): undefined reference to `Chrono::operator+(Chrono::Month const&, int)' /tmp/Chrono-ea8306.o: In function `Chrono::Date::add_day()': Chrono.cpp:(.text+0x549): undefined reference to
My code doesn't compile, Im getting "/tmp/Chrono-ea8306.o: In function `operator++(Chrono::Month&)': Chrono.cpp:(.text+0x1c0): undefined reference to `Chrono::operator+(Chrono::Month const&, int)' /tmp/Chrono-ea8306.o: In function `Chrono::Date::add_day()': Chrono.cpp:(.text+0x549): undefined reference to `Chrono::operator++(Chrono::Month&)' clang: error: linker command failed with exit code 1 (use -v to see invocation) compiler exit status 1" error
---------Chrono.h----------
#ifndef CHRONO_H
#define CHRONO_H
#include "std_lib_facilities.h"
namespace Chrono
{
enum class Month
{
jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};
Month operator++(Month& m); // prefix increment operator
Month operator+(const Month& m, int nMonths); // addition
Month operator+=(Month& m, int nMonths);
class Date
{
public:
class Invalid { }; // to throw as exception
Date(int yy, Month mm, int dd); // check for valid date and initialize
Date(); // default constructor
// the default copy operations are fine
// non modifying operations:
int day() const { return d; }
Month month() const { return m; }
int year() const { return y;}
// modifying operations:
void add_day(); ///add one day
private:
int y;
Month m;
int d;
bool is_valid(int y, Month m, int d); // true for valid date
int nDays(Month m); //how many days in Month m?
};
bool operator==(const Date& a , const Date& b);
bool operator!=(const Date& a , const Date& b);
ostream& operator<<(ostream& os, const Date& d);
istream& operator>>(istream& is, Date& dd);
} // Chrono
#endif
-----------Chrono.cpp----------
#include "Chrono.h"
#include "std_lib_facilities.h"
using namespace Chrono;
Date::Date(int yy, Month mm, int dd)
{
y = yy;
m = mm;
d = dd;
}
Date::Date():y{2001},m{Month::jan},d{1}
{}
int Date::nDays(Month m) {
int days_in_month;
switch (m)
{
case Month::feb: // the length of February varies
days_in_month = 28; //ignore leap year for now
break;
case Month::apr: case Month::jun: case Month::sep: case Month::nov:
days_in_month = 30; // the rest have 30 days
break;
default:
days_in_month = 31; // most months have 31 days
}
return days_in_month;
}
bool Date::is_valid(int y, Month m, int d)
{
// assume that y is valid
if (d<=0) return false; // d must be positive
if (m < Month::jan || Month::dec < m) return false;
if (nDays(m) < d) return false;
return true;
}
Month operator++(Month& m) // prefix increment operator
{
// "wrap around":
m = (m==Month::dec) ? Month::jan : Month(m+1);
return m;
}
Month operator+(const Month& m, int nMonths) // addition
{
int mInt = (int)m + nMonths - 1; ///-1 to align with [0...11] for mod
mInt %= 12; // "wrap around"
return Month(mInt + 1); ///+1 to align back with [1..12]
}
Month operator+=(Month& m, int nMonths) // addition
{
int mInt = (int)m + nMonths - 1; ///-1 to align with [0...11] for mod
mInt %= 12; // "wrap around"
m = Month(mInt + 1); ///+1 to align back with [1..12]
return m;
}
bool operator==(const Date& a, const Date& b)
{
return a.year()==b.year()
&& a.month()==b.month()
&& a.day()==b.day();
}
bool operator!=(const Date& a, const Date& b) { return !(a.year()==b.year()); }
ostream& operator<<(ostream& os, const Date& d)
{
return os << '(' << d.year()
<< ',' << int(d.month())
<< ',' << d.day() << ')';
}
istream& operator>>(istream& is, Date& dd)
{
int y, m, d;
char ch1, ch2, ch3, ch4;
is >> ch1 >> y >> ch2 >> m >> ch3 >> d >> ch4;
if (!is) return is;
if (ch1!= '(' || ch2!=',' || ch3!=',' || ch4!=')') // oops: format error
{
is.clear(ios_base::failbit); // set the fail bit (strange usage of the name "clear")
return is;
}
dd = Date(y, Month(m),d); // update dd
return is;
}
void Date::add_day() {
d++;
if(d > nDays(m))
{
d = 1;
//overloaded operators
//m = m + 1;
//m += 1;
++m;
if(m == Month::jan) y++;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started