//Implementation for Date Class
#include "DateClassSpecifications.h"
#include <iostream>
constint START_YEAR = 1900;
constint 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";
returnfalse;
}
elsereturntrue;
}
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
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.
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.