What is wrong with this Class? LNK 2019 Error

Here are the 3 files my class program is divided into.
File 1 Class Date Specifications
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

#ifndef DATE_H
#define DATE_H	
class Date
{
private:
	static int year;
	static int month;
	static int day;
public:
	Date()
	{
		year = 1900;
		month = 1;
		day = 1;
	}

	Date(int y, int m, int d)
	{
		year = y;
		month = m;
		day = d;
	}

	~Date();

	static bool isValidYear(int y);
	static bool isValidMonth(int m);
	static bool isValidDay(int d);

	void setYear();
	void setMonth();
	void setDay();

	int getYear () const;
	int getMonth () const;
	int getDay () const;
};


#endif 


File 2 Class Implementation

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
//Implementation for Date Class
#include "DateClassSpecifications.h"
#include <iostream>

const int START_YEAR = 1900;
const int CURRENT_YEAR = 2015;


int Date::year = 0;
int Date::month = 0;
int Date::day = 0;

void Date::setYear()
{
	int y = 0;
	do
	{
	std::cout << "Enter the year: ";
	std::cin >> year;
	} while (isValidYear(y));
}

bool Date::isValidYear(int y)
{
	if(y < START_YEAR || y > CURRENT_YEAR)
	{
		std::cout << "That year is not in the acceptable range. Year must 1900 or later and 2015 and ealier.\n";
		return false;
	}
	else
		return true;
}

int Date::getYear () const
{
	return year;
}


File 3 Main Program
1
2
3
4
5
6
7
8
9
10
11
12
#include "DateClassSpecifications.h"
#include <iostream>

int main()
{
	Date userDate;

	userDate.setYear();
	int date = userDate.getYear();
	std::cout << "The date you entered is " << date;
	return 0;
}

I am so tired of these errors; I'm a beginning student and I keep getting all these errors and I don't have enough knowledge yet to know what they mean so please help me out! I linked all the files together by including the DateClassSpecifications.h what kind of linking error is it talking about? I just don't know a lot about C++ and I keep getting errors that require someone to know a lot to even understand them. Help me! Here is the error I get when I try to compile and run the program in VS 2013:
 
Error	1	error LNK2019: unresolved external symbol "public: __thiscall Date::~Date(void)" (??1Date@@QAE@XZ) referenced in function _main	c:\Users\Marcus\documents\visual studio 2013\Projects\Program2\Program2\DateMain.obj	Program2

Last edited on
Wtf I just commented out the destructor and now it works. I know I didn't use the destructor but why would it cause a linking error? Why would an unused destructor cause an error at all?
You do use the destructor. When the variable goes out of scope, the destructor is automatically called.
Because you haven't provided a body to that function (and by declaring it you eliminated the one provided by the compiler), you've got a linker error.
I believe it is always best to just have the destructor in the cpp file, like this -

1
2
3
4
ClassName::~ClassName()
{
    //Do Nothing
}


atleast thats what I always do.
or just mark it in the header ~foo() = default;
(or nothing at all, if you don't need special behaviour)
Last edited on
Just to reiterate and expand on what's already been said:

1) The destructor is always called when an object is destroyed. If the object has been created on the stack, as a local variable, then that happens when it goes out of scope. If you've dynamically allocated the memory for the object on the heap, then it happens when you destroy that object and free up the memory.

2) If you don't declare and define a destructor, then the compiler will create a default one for you that, essentially, does nothing. However, if you declare a destructor - as you do on line 25 of your first piece of code - then you're telling the compiler that you're going to define your own destructor. If you don't then actually define it, you'll get that error.

Hope that makes things clear?
Topic archived. No new replies allowed.