[問題] structure vector沒有辦法在function使用

看板C_and_CPP (C/C++)作者 (若自礌)時間11年前 (2015/03/29 16:45), 編輯推噓2(2013)
留言15則, 6人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 問題(Question): structure vector沒有辦法在function裡call structure的元素 我要做的事情類似這樣 struct stu { int eng; }; void change(vector<stu> *stulist, int length) { if (stulist[0].eng == stulist[1].eng)//這行有問題編譯不過 //do something } 如果要修正的話請問各位大大會怎麼樣修正呢? 錯誤結果(Wrong Output): error C2039: 'eng' : 不是 'std::vector<stu,std::allocator<_Ty>>' 的成員 煩請各位大大幫忙解惑! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 39.12.178.61 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1427618723.A.725.html

03/29 16:52, , 1F
傳 reference => vector<stu> & stulist
03/29 16:52, 1F

03/29 17:01, , 2F
感謝樓上的回答,這也是個一勞永逸的方法!不過有不用
03/29 17:01, 2F

03/29 17:02, , 3F
call by reference的辦法嗎?
03/29 17:02, 3F

03/29 17:12, , 4F
stulist[0]和stulist[0][0]的差異能分得出來,這題就解了
03/29 17:12, 4F

03/29 17:26, , 5F
樓上我不是很明白,因為我change這個function裡面會對
03/29 17:26, 5F

03/29 17:27, , 6F
stulist裡面的元素做修改,且stulist是一個一維陣列,
03/29 17:27, 6F

03/29 17:27, , 7F
所以我原先的想法傳入一個vector指標應該沒有錯吧?
03/29 17:27, 7F

03/29 17:29, , 8F
樓上大大好像認為我傳的是二維陣列?還是我有東西沒搞懂?
03/29 17:29, 8F

03/29 17:39, , 9F
四樓應該是想說「stulist[0] 跟 (*stulist)[0] 的差別」
03/29 17:39, 9F

03/29 17:43, , 10F
"vector"可以用做一維陣列,但"vector指標"並不是
03/29 17:43, 10F

03/29 18:01, , 11F
原來如此!我大概明白了!感謝樓上各位大大!
03/29 18:01, 11F

03/29 18:31, , 12F
唉,陣列指標隱性轉換害人不淺
03/29 18:31, 12F

03/29 18:32, , 13F
其實你寫成stulist->at(0).eng就會對了
03/29 18:32, 13F

03/29 18:33, , 14F
不過既然都用c++了 用by reference吧
03/29 18:33, 14F

04/02 22:36, , 15F
指標不是用箭頭嗎
04/02 22:36, 15F
文章代碼(AID): #1L5xkZSb (C_and_CPP)
文章代碼(AID): #1L5xkZSb (C_and_CPP)