unresolved external symbol LNK2019

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.
It says that Date::getMonth(), Date::getDay() and Date::Date() are not defined. Look in Date.cpp and see that they are all there.
Yeah they're all in Date.cpp. Also compiled and tested them separately and they worked fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Date::Date()
{
	time_t t = time(0); // get time now
	struct tm * now = localtime(&t);

	month = now->tm_mon + 1;
	day = now->tm_mday;
	year = now->tm_year + 1900;
	dayOfWeek = now->tm_wday;
}

int Date::getMonth() const {
	return month;
}

int Date::getDay() const {
	return day;
}
Are those all the linker errors or does it also mention getYear, etc. ?
It seems strange that just three would be missing but the others would be there.

How are you compiling it?
Ah whoops, you're right missed three other errors.

Other three errors:

Error LNK2019 unresolved external symbol "public: int __thiscall Date::getYear(void)const " (?getYear@Date@@QBEHXZ) referenced in function "public: int __thiscall Birthday::getAge(void)const " (?getAge@Birthday@@QBEHXZ)

Error LNK2019 unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Date::monthName(void)const " (?monthName@Date@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "public: virtual void __thiscall Birthday::printDate2(void)const " (?printDate2@Birthday@@UBEXXZ)

Error LNK1120 5 unresolved externals

getYear, and monthName are also defined. I'm using visual studio, I just click Build Solution to compile.

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
int Date::getYear() const {
	return year;
}

string Date::monthName() const {
	string monthName;
	if (month == 1) {
		monthName = "January";
	}
	else if (month == 2) {
		monthName = "February";
	}
	else if (month == 3) {
		monthName = "March";
	}
	else if (month == 4) {
		monthName = "April";
	}
	else if (month == 5) {
		monthName = "May";
	}
	else if (month == 6) {
		monthName = "June";
	}
	else if (month == 7) {
		monthName = "July";
	}
	else if (month == 8) {
		monthName = "August";
	}
	else if (month == 9) {
		monthName = "September";
	}
	else if (month == 10) {
		monthName = "October";
	}
	else if (month == 11) {
		monthName = "November";
	}
	else if (month == 12) {
		monthName = "December";
	}
	return monthName;
}

So you've created a project and all the cpp files and the header files are part of the project? In that case, it should work automatically and I don't know what's wrong.

You could maybe do without the switch:
1
2
3
4
5
6
string Date::monthName() const {
	static const char *names[12] = {
		"January","February","March","April","May","June",
		"July","August","September","October","November","December"};
	return names[month];
}

Last edited on
That does look a lot neater, thanks. Yeah, I added them all into the project to start with, and rechecking now they all are listed to be a part of the project. Not sure what the problem is, think I'll try recreating the project and adding everything again.
Well not sure where I went wrong the first time, but everything appears to be compiling fine now after creating a new project and re-adding the files. Thanks for the help!
Topic archived. No new replies allowed.