How do i fix this line Appointment a1dd tt nn of code in mai
How do i fix this line: Appointment a1(dd, tt, nn);
of code in main below so i don\'t receive this error:
/tmp/ccCH7Ffz.o: In function `main\':
hw5-pr2.cpp:(.text+0x378): undefined reference to `Chrono::Appointment::Appointment(Chrono::Date, Chrono::Time, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)\'
collect2: error: ld returned 1 exit status
Below is main, chrono.cpp and chrono.h
I am trying to create an appointment with a user defined date time and name but the compiler wont let me.
----------------------------------------------------------------------------main---------------------------------------------------------------------------
int main() {
vector<Appointment>Datebook;
/*string fileName;
string line;
cout<<\"Please name a file to store Datebook into: \";
cin>>fileName;
ofstream file;
file.open(fileName);
if(file.is_open())
{
while(getline(file,line))
{
cout<<line;
}
}
file.close();*/
bool cont = true; //Continue inputting dates
while(cont){ //Ask for dates until user stops program
string choice; //Stores user inputs
int d, m, y; //Day, month, and year ints
cout<<\"\ Please be aware that leapyears and specific day amounts \ for months are not yet accounted for.\"<<endl<<endl;
cout<<\"\ Please enter a day (1-31), month (1-12) and year: \";
cin>>d>>m>>y;
cout<<endl;
while(!is_date(y,Date::Month(m),d)|| cin.fail()){ //Checks to see if input values make a valid date or if they are even integers
cin.clear(); //Clears cin for new input
cin.ignore(100, \'\ \');
cout<<\"Invalid response, Please enter a day (1-31), month (1-12) and year: \";
cin>>d>>m>>y;
cout<<endl;
}
Date dd(y, Date::Month(m), d);
/*cout<<\"\ Now please enter the corresponding number to the constructor you\'d like to use.\";
cout<<\"\ (1)zero-argument\ (2)(Year,Day,Month) input format\ (3)(Month,Day,Year) input format\ Constructor to use: \";
cin>>choice;
while(choice != \"1\" || choice != \"1\" ||choice != \"3\"){ //Loop until user inputs a valid choice
if(choice == \"1\"){ //User chose 1, default constructor is called with d1
Date d1;
cout<<endl<<d1;
break;
}
else if(choice == \"2\"){ //User chose 2, existing 3-argument constructor is called with d2
Date d2(y, Date::Month(m), d);
cout<<endl<<d2;
break;
}
else if(choice==\"3\"){ //User chose 3, new 3-argument constructor is called with d3
Date d3(Date::Month(m), d, y);
cout<<endl<<d3;
break;
}
else { //User didn\'t choose 1-3
cout<<\"Invalid choice, please select 1-3: \";
cin>>choice;
}
}*/
//--------------------------------------------------------------Time Code-----------------------------------------------------------------------------
int h, min, s, a; //hour, minute, second and ampm ints
cout<<\"\ \ Please enter an hour (0-23), a minute (0-59), a second (0-59) \ and a value for AM (1) or PM (2): \";
cin>>h>>min>>s>>a;
cout<<endl;
while(!is_time(h, min, s, Time::AmPm(a)) || cin.fail()){ //Checks to see if input values make a valid time or if they are even integers
cin.clear();
cin.ignore(100, \'\ \');
cout<<\"Invalid response, Please enter an hour (0-23), a minute (0-59), a second (0-59) \ and a value for AM (1) or PM (2): \";
cin>>h>>min>>s>>a;
cout<<endl;
}
Time tt(h, min, s);
string nn;
cout<<\"\ \ Please enter a name for the appointment: \";
cin>>nn;
cout<<endl;
Appointment a1(dd, tt, nn);
...}
-----------------------------------------------------------------------------chrono.cpp-----------------------------------------------------------
#include \"Chrono.h\"
namespace Chrono {
// member function definitions:
//------------------------------------------------------------------------------
Date::Date(int yy, Month mm, int dd)
: y(yy), m(mm), d(dd)
{
if (!is_date(yy,mm,dd)) throw Invalid();
}
Date::Date(Month mm, int dd, int yy) //New date member function with
: y(yy), m(mm), d(dd) //switched parameters
{
if (!is_date(yy,mm,dd)) throw Invalid();
}
//------------------------------------------------------------------------------
const Date& default_date()
{
static const Date dd(2001,Date::jan,1); // start of 21st century
return dd;
}
//------------------------------------------------------------------------------
Date::Date()
:y(default_date().year()),
m(default_date().month()),
d(default_date().day())
{
}
Time::Time(int hh, int mm, int ss) //Time member function for 24-hour format
: h(hh), m(mm), s(ss)
{
if(!is_time(hh,mm,ss)) throw Invalid();
}
Time::Time(int hh, int mm, int ss, AmPm aa) //Time member function for 12-hour format
: h(hh), m(mm), s(ss), a(aa)
{
if(!is_time(hh,mm,ss,aa)) throw Invalid();
}
const Time& default_time() //default time
{
static const Time tt(0,0,0,Time::AM);
return tt;
}
Time::Time() //define default time
:h(default_time().hour()),
m(default_time().minute()),
s(default_time().second()),
a(default_time().ampm())
Appointment::Appointment(Date dd, Time tt, string nn)
: d(dd), t(tt), n(nn)
{
//if(!is_appointment(dd,tt,nn)) throw Invalid();
}
...}
--------------------------------------------------------------------chrono.h---------------------------------------------------------------
#include <iostream>
using namespace std;
//------------------------------------------------------------------------------
namespace Chrono {
//------------------------------------------------------------------------------
class Date {
public:
enum Month {
jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};
class Invalid { }; // to throw as exception
Date(int y, Month m, int d); // check for valid date and initialize
Date(Month mm, int dd, int yy); //ADDED CONSTRUCTOR
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(int n);
void add_month(int n);
void add_year(int n);
private:
int y;
Month m;
int d;
};
class Time {
public:
enum AmPm {
AM=1, PM
};
class Invalid { };
Time(int h, int m, int s); //constructor for 24 hour format
Time(int h, int m, int s, AmPm a); //constructor for 12 hour formant
Time(); //default constructor
int hour() const { return h; } //non-modifying operations
int minute()const { return m; }
int second() const { return s; }
AmPm ampm() const { return a; }
private:
int h;
int m;
int s;
AmPm a;
};
class Appointment {
public:
class Invalid { };
Appointment(Date d, Time t, string n);
Appointment();
Date date() const { return d; }
Time time() const { return t; }
string name() const { return n; }
Date d;
Time t;
string n;
};
//------------------------------------------------------------------------------
bool is_date(int y, Date::Month m, int d); // true for valid date
bool is_time(int h, int m, int s, Time::AmPm a); // true for valid time
bool is_time(int h, int m, int s);
Time correct_ampm(Time tt); // Turns 24 hour format into 12 hour format with appropriate am or pm
//------------------------------------------------------------------------------
bool leapyear(int y); // true if y is a leap year
//------------------------------------------------------------------------------
bool operator==(const Date& a, const Date& b);
bool operator!=(const Date& a, const Date& b);
bool operator==(const Time& a, const Time& b); //overloads == operator to compare times
bool operator!=(const Time& a, const Time& b); //overloads != operator to compare times
//------------------------------------------------------------------------------
ostream& operator<<(ostream& os, const Time& t); //overloads << to print times
ostream& operator<<(ostream& os, const Date& d);
ostream& operator<<(ostream& os, const Appointment& a);
istream& operator>>(istream& is, Date& dd);
//------------------------------------------------------------------------------
} // Chrono
Solution
Chrono.cpp
I continue to get this error, or variations of saying it doesnt match the .h file.
Both files have #include string and the .cpp file is in the std name space. I have also tried using std::string in the header file. Nothing I have done has worked so far. Any help is appreciated. Also note that Date is defined elsewhere and is working correctly.




