今天,练习编程时,照着书本敲代码,居然出现了问题,太诡异了。而且出现的是:error LNK2019: 无法解析的外部符号
这样诡异的问题,着实困扰了我10分钟。然后我找到了解决办法——补充默认不带参数的构造函数的函数体。
######################################
方法如下:
######################################
1.出问题的代码如下
#include "stdafx.h"
#include <iostream>
using namespace std;
class DayofYear
{
public:
DayofYear(int the_month, int the_day);
DayofYear();
void input();
void output();
int get_month();
int get_day();
private:
void check_date();
int month;
int day;
};
bool equal(DayofYear date1, DayofYear date2);
int _tmain(int argc, _TCHAR* argv[])
{
DayofYear today, bach_birthday(3, 21);
cout << "Enter today's date: \n";
today.input();
cout << "Today's date is ";
today.output();
cout << "J. S. Bach's birthday is ";
bach_birthday.output();
if ( equal(today, bach_birthday))
{
cout << "Happy Birthday Johann Sebastian!\n";
}
else
{
cout << "Happy Unbirthday Johann Sebastian!\n";
}
system("pause");
return 0;
}
bool equal(DayofYear date1, DayofYear date2)
{
return ( date1.get_month() == date2.get_month() &&
date1.get_day() == date2.get_day() );
}
DayofYear::DayofYear(int the_month, int the_day)
: month(the_month), day(the_day)
{
check_date();
}
//DayofYear::DayofYear()
//{
//
//}
int DayofYear::get_month()
{
return month;
}
int DayofYear::get_day()
{
return day;
}
void DayofYear::input()
{
cout << "Enter the month as a number: ";
cin >> month;
cout << "Enter the day of the month: ";
cin >> day;
}
void DayofYear::output()
{
cout << "month = " << month
<< ", day = " << day << endl;
}
void DayofYear::check_date()
{
}
2.解决问题的代码如下,只改了一个地方,就是去掉上面代码的注释
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class DayofYear
{
public:
DayofYear(int the_month, int the_day);
DayofYear();
void input();
void output();
int get_month();
int get_day();
private:
void check_date();
int month;
int day;
};
bool equal(DayofYear date1, DayofYear date2);
int _tmain(int argc, _TCHAR* argv[])
{
DayofYear today, bach_birthday(3, 21);
cout << "Enter today's date: \n";
today.input();
cout << "Today's date is ";
today.output();
cout << "J. S. Bach's birthday is ";
bach_birthday.output();
if ( equal(today, bach_birthday))
{
cout << "Happy Birthday Johann Sebastian!\n";
}
else
{
cout << "Happy Unbirthday Johann Sebastian!\n";
}
system("pause");
return 0;
}
bool equal(DayofYear date1, DayofYear date2)
{
return ( date1.get_month() == date2.get_month() &&
date1.get_day() == date2.get_day() );
}
DayofYear::DayofYear(int the_month, int the_day)
: month(the_month), day(the_day)
{
check_date();
}
DayofYear::DayofYear()//是的,没错,就是这里!!!!!!!!!!!
{
}
int DayofYear::get_month()
{
return month;
}
int DayofYear::get_day()
{
return day;
}
void DayofYear::input()
{
cout << "Enter the month as a number: ";
cin >> month;
cout << "Enter the day of the month: ";
cin >> day;
}
void DayofYear::output()
{
cout << "month = " << month
<< ", day = " << day << endl;
}
void DayofYear::check_date()
{
}
好了,现在大家知道,怎么解决问题了吧。
-------------------------------------