[問題] 二維運算符號重載無法賦值?

看板C_and_CPP (C/C++)作者 (我不要成為廢文王)時間10年前 (2015/09/22 13:12), 10年前編輯推噓1(104)
留言5則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Xcode 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) None 問題(Question): 抱歉又要打擾各位了><這次搜尋了一下感覺很少人提到的樣子... 一般如果要把class做成像二維矩陣a[1][2]方式使用 可以利用兩次重載達成 但是如果需要進行賦值compiler卻表示要重載“=” 這是正常的嗎? 餵入的資料(Input): vector<Data>table; table[1][1]=1; 預期的正確結果(Expected Output): 正式存入1到指定index 錯誤結果(Wrong Output): No viable overloaded '=' 程式碼(Code):(請善用置底文網頁, 記得排版) class Data { public: class Proxy { public: Proxy(int* _array) : _array(_array) { } int operator[](int index)const { return _array[index]; } private: int* _array; }; Proxy operator[](int index)const { return Proxy(_arrayofarrays[index]); } private: int** _arrayofarrays; }; 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.25.100 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1442898741.A.D56.html

09/22 13:21, , 1F
正常, 取值跟存值是兩回事
09/22 13:21, 1F

09/22 14:35, , 2F
不確定你的用意. 你確定你要用 vector ?
09/22 14:35, 2F

09/22 14:36, , 3F
如果你不是要用 vector, 你需要加上非 const 的operator[]
09/22 14:36, 3F
目前確實是要用vector 所以還要另外在vector重載一次“="這樣? ※ 編輯: dinex (140.112.25.100), 09/22/2015 17:08:29

09/22 17:14, , 4F
那你知道你的 table 是三維的嗎?
09/22 17:14, 4F
主要是貪圖vector可以任意增加維度大小 抱歉之前想不清楚 ※ 編輯: dinex (140.112.25.100), 09/22/2015 17:52:02

09/22 18:08, , 5F
所以你到底想要幾維的阿? 用 vector<vector<int>> table ?
09/22 18:08, 5F
啊真的是智障了... 那應該只要用一維寫Data就好了 謝謝大大們再次包容== ※ 編輯: dinex (140.112.25.100), 09/22/2015 18:26:09
文章代碼(AID): #1M0ECrrM (C_and_CPP)
文章代碼(AID): #1M0ECrrM (C_and_CPP)