看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) Win7 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) Visual studio 2013 問題(Question): 要建一個class matrix 能夠透過constructor生成二維陣列 有無參數跟有參數兩種constructor class matrix { private: int** m; int size; public: //parameter constructor matrix(int s) { size = s; m = new int*[size]; for (int i = 0; i<size; i++) m[i] = new int[size]; } //default constructor matrix() { m = new int*[size]; for (int i = 0; i<size; i++) m[i] = new int[size]; } } ----------------------------------------- 在主程式裡頭不曉得要怎麼達到下面這種效果 matrix* mArr1 = new matrix[10]; //calling default constructor matrix mArr2[5]; //calling default constructor 資料成員雖然有size可是沒初值用在default constructor上面無作用 要做像是 matrix mArr2[5]這樣事情時就無法 感覺matrix(){}有點像是多打的 想了很久不知道怎麼解決 C++新手請求各位高手幫忙@@ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.241.16.145 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1478348296.A.FCD.html
steve1012: 看不太懂你想要幹嘛 11/05 21:00
steve1012: Marr1 跟marr2有啥關聯 11/05 21:01
steve1012: 幾個看到的問題1.size 用size_t 2. 為何不用vector 3. 11/05 21:04
steve1012: 值沒有出始化 11/05 21:04
steve1012: Matrix 剛開始會是一堆garbage 11/05 21:05
nick5130: operator overloading也沒辦法做到你想要的樣子吧? 11/05 21:59
nick5130: 而且你希望的matirx[10]應該是一個10x10 matrix? 11/05 21:59
nick5130: 如果是的話你直接把你希望的樣子改成類似new matrix(10) 11/05 22:00
nick5130: 和mArr2(5) 看起來應該就會動了 有錯請指正 thx 11/05 22:01
nick5130: 我這種寫法等於你的default constructor是沒有用的 11/05 22:02
nick5130: 不過你的default constructor的size也未定義 也是不能用 11/05 22:02