[問題] 如何在member function 中創造新物件

看板C_and_CPP (C/C++)作者 (steve)時間14年前 (2011/12/28 16:05), 編輯推噓0(0014)
留言14則, 5人參與, 最新討論串1/2 (看更多)
我想寫一個class 其中有一個member function 可以創造出一個新的物件 比如說我的class 叫作 Integer 我想寫一個member function 叫做 addInteger 可以創造出一個新的Integer物件,內容是我所指定的 這要怎麼辦到呢? 要是在member function 中declare的物件不是只能存活到meber function 結束為止? --------------------------------------------------------------------------- 想再問一個vector在class中使用的方法 我要怎麼讓一個function 回傳vector 呢? 並且我宣告一個member data 為vector的方式哪裡錯了 我爬過文 也用了member initializer 了 以下是連結 http://codepad.org/bHMJOD7J 標頭檔 http://codepad.org/wGTV9IiI CPP檔 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.7.214

12/28 16:14, , 1F
你想怎麼用這隻物件?
12/28 16:14, 1F
其實我是想在這個class裡面宣告一組vector存放數字 然後我想要求兩個物件裡的vector的交集 把它存在第三個vector裡面 可是我不知道怎麼宣告這個新的vector ※ 編輯: steve1012 來自: 140.112.7.214 (12/28 16:24) 啊我好像想到了..這好像是一個蠢問題... 我就回傳一組vector就好了.. 可是要怎麼回傳一整組vector呢??? ※ 編輯: steve1012 來自: 140.112.7.214 (12/28 16:27) ※ 編輯: steve1012 來自: 140.112.7.214 (12/28 16:48)

12/28 17:05, , 2F
試試這個 vector<int>& munionSet(IntegerSet);
12/28 17:05, 2F

12/28 17:06, , 3F
這個就當你的 vector 元素型態是 int 好了。( ̄ー ̄;)
12/28 17:06, 3F

12/28 17:10, , 4F
增加一個元素到 vector「http://ppt.cc/CZ@6(MSDN)」
12/28 17:10, 4F
摁摁 我又想到可以傳vector給function 就不用回傳vector了 可是compiler一值不認識vector!!! 是不能傳Vector 當參數嗎? 我是用 Visual studio 2010 ※ 編輯: steve1012 來自: 140.112.7.214 (12/28 17:19)

12/28 17:26, , 5F
請服用這個「using std::vector;」(∩_∩)
12/28 17:26, 5F

12/28 17:28, , 6F
忘了說明…那是加在標頭檔內的 ( ̄ー ̄;)
12/28 17:28, 6F

12/28 19:52, , 7F
為啥 vector<T> 裡面可以不用傳樣版參數 T ??
12/28 19:52, 7F

12/28 19:53, , 8F
'__gnu_debug_def::vector' without an argument list
12/28 19:53, 8F

12/28 19:54, , 9F
妳的 vector<T> 要告訴它到底是哪個型別的 vector
12/28 19:54, 9F

12/28 19:54, , 10F
vector<int> 才是一個 int 的 vector
12/28 19:54, 10F

12/28 19:55, , 11F
並沒有 vector 這個型別,請為它加入樣版參數,感恩
12/28 19:55, 11F

12/28 20:08, , 12F
static....
12/28 20:08, 12F

12/28 21:49, , 13F
class內的member不都是共用的?
12/28 21:49, 13F

12/28 21:50, , 14F
有必要特意去思考怎麼傳遞嗎?你的用途不需要這麼複雜..
12/28 21:50, 14F
文章代碼(AID): #1E-itFXG (C_and_CPP)
文章代碼(AID): #1E-itFXG (C_and_CPP)