[問題] class的一點問題

看板C_and_CPP (C/C++)作者 (>.*)時間10年前 (2016/04/12 19:46), 編輯推噓0(003)
留言3則, 2人參與, 最新討論串1/1
大家好 我是C++新手 最近學到class我有些問題 這是.h檔中的程式碼(範例) class asd{ public: asd(); asd(int,int,int); int a; int b; int c; int fun1(); int fun2(); int fun3(); }; asd::asd(){ //預設 a=1; b=2; c=3; } asd::asd(int fun1,int fun2,int fun3) { a=fun1; b=fun2; c=fun3; } int asd::fun1(){ return a; } int asd::fun2() { return b; } int asd::fun3() { return c; } 這個程式碼在.cpp中執行的結果是: 我可以asd a1(變數,變數,變數)或者是直接asd a1(預設中a=1,b=2,c=3) 但是我沒辦法只宣告其中的一兩個變數 例如:asd(變數,變數) 然後第三個變數會等於預設中的3 執行會跑出error: no matching function for call to 'asd::asd(int, int)'的錯誤訊息 請問這要如何做到只輸入其中幾個變數呢? 請各位高手指教 謝謝 -- Sent from my Windows -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.236.54.231 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1460461599.A.54D.html

04/12 19:50, , 1F
用 Default arguments
04/12 19:50, 1F

04/12 19:50, , 2F
宣告改成 class asd {... asd(int=1,int=2,int=3); ...}
04/12 19:50, 2F

04/12 20:16, , 3F
成功了喔 感謝b大
04/12 20:16, 3F
文章代碼(AID): #1N3E0VLD (C_and_CPP)
文章代碼(AID): #1N3E0VLD (C_and_CPP)