看板 C_and_CPP 關於我們 聯絡資訊
不好意思 一個的例子 想請問一下各位 底下 MAIN.CPP底線部份要如何設定才能正確的初始化? 應該說在類別裡面用列舉不是很懂 謝謝~ 程式碼寫得應該不長 煩請指教一下 //main.cpp #include"date.h" #include<iostream> int main() { Date today(11,_____________,2012); std::cout << today.year() << '\n'; return 0; } //date.h class Date{ public: enum Month{jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec}; Date(int dd = 0, Month mm = Month(0), int yy = 0); int day() const {return d;}; Month month() const {return m;}; int year() const {return y;}; static void set_default(int, Month, int); private: int d, y; Month m; static Date defalut_date; }; //date.cpp #include"date.h' Date::Date(int dd, Month mm, int yy) { if(yy==0) y = default_date.year(); if(mm==0) m = default_date.month(); if(dd==0) d = default_date.day(); d = dd; m = mm; y = yy; } Date Date::defalut_date(1,dec,1990); void Date::set_default(int d, Month m, int y) { Date::default_date = Date(d,m,y); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.60.0.221
shadow0326:Date today(11, Date::apr, 2012); 04/11 17:50
snoopy0907:謝謝 我也是那樣設 但是一直會有錯 04/11 18:21
snoopy0907:date_main.cpp:(.text+0x29): undefined reference to 04/11 18:22
snoopy0907:`Date::Date(int, Date::Month, int)' 04/11 18:23
snoopy0907:collect2: ld returned 1 exit status 04/11 18:23
purincess:有連著Date.cpp一起compile(其實應該是link)嗎@@? 04/11 18:30
snoopy0907:謝謝 04/11 18:58