error LNK2019: 無法解析的外部符號 (C++程式設計出現問題的解決辦法)
阿新 • • 發佈:2019-01-10
今天,練習程式設計時,照著書本敲程式碼,居然出現了問題,太詭異了。而且出現的是: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() { }
好了,現在大家知道,怎麼解決問題了吧。
-------------------------------------