Hey, first time posting here, I read the "read here before posting" but apologies if I mess up.
I'm trying out inheritance for the first time and came across a set of LNK2019 errors which I haven't previously encountered. Tried searching but couldn't find anything that helped.
Errors:
Error LNK2019 unresolved external symbol "public: int __thiscall Date::getMonth(void)const " (?getMonth@Date@@QBEHXZ) referenced in function "public: int __thiscall Birthday::getAge(void)const " (?getAge@Birthday@@QBEHXZ
Error LNK2019 unresolved external symbol "public: __thiscall Date::Date(void)" (??0Date@@QAE@XZ) referenced in function "public: __thiscall Birthday::Birthday(int,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int)" (??0Birthday@@QAE@HHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z)
Error LNK2019 unresolved external symbol "public: int __thiscall Date::getDay(void)const " (?getDay@Date@@QBEHXZ) referenced in function "public: int __thiscall Birthday::getAge(void)const " (?getAge@Birthday@@QBEHXZ)
Code:
Date.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
|
#ifndef DATE_H_
#define DATE_H_
#include <string>
using namespace std;
class Date
{
public:
Date();
Date(int month, int day);
Date(int month, int day, int year);
int getMonth() const;
int getDay() const;
int getYear() const;
void printDate1() const;
void printDate2() const;
int daysSinceJan1() const;
long long daysSince() const;
bool isLeapYear() const;
string monthName() const;
string dayName() const;
bool equals(const Date& other) const;
long long compareTo(const Date& other) const;
void read();
long long giveNumber();
protected:
int month;
int day;
int year;
int dayOfWeek; //day of week in numerical format, 0 = sunday, 1= monday, ... , 6 = saturday
};
#endif DATE_H_
|
Birthday.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
|
#include "Date.h"
#include <string>
class Birthday : public Date {
public:
Birthday();
/**
* Constructor: birthday will always be this year
* @param m month
* @param d day
* @param n name
* @param y birth year
*/
Birthday(int month, int day, std::string name, int birthyear);
/**
* Calculates or gets the age of person with birthday
* @return age of person with "this" birthday
*/
int getAge() const;
/**
* Gets the name of person with birthday
* @return name of person with "this" birthday
*/
std::string getName() const;
/**
* Sets the name of person with birthday
* @param n the name of person with "this" birthday
*/
void setName(std::string name);
/**
* Sets the year of birth of person with birthday
* @param y the year the person with "this"
* birthday was born
*/
void setBirthYear(int birthyear);
/**
* Extends printDate2 in base class and includes
* " is so and so's birthday" at the end
*/
virtual void printDate2() const;
private:
std::string name; // name of person with this birthday
int birthYear; // birth year of the person
};
|
Birthday.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 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
|
#include "Birthday.h"
#include "Date.h"
#include <string>
#include <ctime>
#include <cassert>
#include <iostream>
Birthday::Birthday() {
time_t t = time(0); // get time now
struct tm * now = localtime(&t);
month = now->tm_mon + 1;
day = now->tm_mday;
birthYear = now->tm_year + 1900;
dayOfWeek = now->tm_wday;
name = "";
}
Birthday::Birthday(int monthIn, int dayIn, std::string nameIn, int birthyearIn) {
month = monthIn;
day = dayIn;
name = nameIn;
birthYear = birthyearIn;
}
int Birthday::getAge() const {
int age = 0; //january 1st 1990 january 2st 2008
Date today;
if (today.getMonth() == month) {
if (today.getDay() >= day) {
age = today.getYear() - birthYear;
}
else {
age = today.getYear() - birthYear - 1;
}
}
else if (today.getMonth() > month) {
age = today.getYear() - birthYear;
}
else {
age = today.getYear() - birthYear - 1;
}
return age;
}
std::string Birthday::getName() const {
return name;
}
void Birthday::setName(string nameIn) {
name = nameIn;
}
void Birthday::setBirthYear(int birthYearIn) {
birthYear = birthYearIn;
}
void Birthday::printDate2() const {
cout << monthName() << " " << day << ", " << birthYear << endl;
}
|
Can post the implementation of Date too if necessary, didn't want to add more than needed.