Hi guys! currently making a Date Class and as I am compiling in g++ I am getting the following errors when I compute my Datemain.cpp file:
[rrazzak@cloudland ~]$ g++ -g -Wall -o ExecutableFileName Datemain.cpp
/tmp/cc4wbMX0.o: In function `main':
/home/rrazzak/Datemain.cpp:7: undefined reference to `Date::Date()'
/home/rrazzak/Datemain.cpp:8: undefined reference to `Date::increment()'
/home/rrazzak/Datemain.cpp:10: undefined reference to `Date::display1()'
/home/rrazzak/Datemain.cpp:11: undefined reference to `Date::increment()'
/home/rrazzak/Datemain.cpp:14: undefined reference to `Date::display2()'
/home/rrazzak/Datemain.cpp:15: undefined reference to `Date::increment()'
/home/rrazzak/Datemain.cpp:18: undefined reference to `Date::Date(int, int, int)'
/home/rrazzak/Datemain.cpp:19: undefined reference to `Date::increment()'
/home/rrazzak/Datemain.cpp:20: undefined reference to `Date::display1()'
/home/rrazzak/Datemain.cpp:23: undefined reference to `Date::increment()'
/home/rrazzak/Datemain.cpp:24: undefined reference to `Date::display2()'
collect2: ld returned 1 exit status
Yet when ever I do this: g++ -g -Wall -o ExecutableFileName Datemain.cpp Date.cpp, i am able to compile.. I assume you do not include the cpp file in main because you already include the header file, but I don't quite understand the undefined reference error I am getting
Date.cpp file:
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include "Date.h" //includes the Date header file
#include <iostream>
#include <cassert> //lets us use assertions in C ++
using namespace std;
//default constructor with default values
Date::Date()
{
month = 1;//default month value
day = 1;//default day value
year = 2000;//default year value
}
//postcondition: a Date with a month, day and year has been created
//precondition: Date will check if any of the conditions have been violated
Date::Date(int Month,int Day,int Year)
{
if((Month < 1||Month > 12)||(Day < 1||Day > 31)||(Year < 1900||Year > 2020))
{
std::cout<<"Invalid"<<std::endl;
}
else
{
month = Month;
day = Day;
year = Year;
}
}
//postcondition: Date checked that the code does not violate any of the parameters
//precondition: Day will have been incremented by 1
void Date::increment()
{
//month += 1;
//assert(month >= 1 && month <= 12);
day += 1;
assert(day >= 1 && day <= 31);
if(month == 2 && day == 28 || day == 29)
{
if(year % 4 || year % 400)
{
std::cout<<"Thats a Leap Year"<<std::endl;
//month += 1;
day += 1 ;
//year++;
assert(day >= 1 && day <= 31);
assert(month >= 1 && month <= 12);
}
}
}
//postcondition: Day has been incremented by 1
void Date::display1()
{
std::cout<<month<<'/'<<day<<'/'<<year<<std::endl;
}
//postcondition: Date has been displayed in number format
void Date::display2()
{
string Month;
switch(month)
{
case 1:
Month="January";
break;
case 2:
Month="February";
break;
case 3:
Month="March";
break;
case 4:
Month="April";
break;
case 5:
Month="May";
break;
case 6:
Month="June";
break;
case 7:
Month="July";
break;
case 8:
Month="August";
break;
case 9:
Month="September";
break;
case 10:
Month="October";
break;
case 11:
Month="November";
break;
case 12:
Month="December";
break;
}
std::cout<<Month<<'/'<<day<<'/'<<year<<std::endl;
}
//postcondition: Date is displayed in word format
|
Date.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#ifndef DATE_H
#define DATE_H
class Date
{
private:
int month;
int day;
int year;
public:
Date();
Date(int month,int day,int year);
void display1();
void display2();
void increment();
};
#endif //DATE_DATE
|
Datemain.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
|
#include <iostream>
#include "Date.h" //includes Date header file
#include <string>
using namespace std;
int main()
{
Date d;
d.increment();
d.display1();
d.increment();
std::cout<<std::endl;
d.display2();
d.increment();
std::cout<<'\n'<<std::endl;
Date today(1,29,2017);
today.increment();
today.display1();
std::cout<<std::endl;
today.increment();
today.display2();
std::cout<<"\n\n"<<std::endl;
std::cout<<"ALL TESTS PASSED! YAAY!"<<std::endl;
return 0;
}
|