好的,我正在實現一個動態的2維矩陣類。對於一個基礎,這是我到目前爲止有:無法以任何方式讀取函數類
template <typename Type>
class dyMatrix {
private:
Type *mat;
int lines, columns;
int length;
void assimilate (const dyMatrix<Type>& that) {
lines = that.lines;
columns = that.columns;
length = that.length;
}
public:
dyMatrix (int height, int width)
: lines(height), columns(width), mat(0)
{
length = lines * columns;
mat = new Type[length];
};
// ---
int getLines() {
return lines;
};
int getColumns() {
return columns;
};
int getLength() {
return length;
}
// ---
Type& operator() (int i, int j) {
return mat[j * columns + i];
};
Type& operator() (int i) {
return mat[i];
};
// ---
dyMatrix (const dyMatrix<Type>& that) {
this->assimilate(that);
// ---
mat = new Type[length];
for (int i = 0; i < length; i++) {
mat[i] = that.mat[i];
}
};
dyMatrix& operator= (const dyMatrix<Type>& that) {
Type *local_mat = new Type[that.length];
for (int i = 0; i < that.length; i++) {
local_mat[i] = that.mat[i];
}
// ---
this->assimilate(that);
delete[] mat;
mat = local_mat;
// ----
return *this;
};
~dyMatrix() {
delete mat;
};
};
我的問題是,試圖讀取輸入到它時,我一直有一些特定的問題。例如,我主要有如下():
int main() {
int lanes, cols;
cin >> lanes >> cols;
dyMatrix<int> map(lanes, cols);
/* CONTINUE READING */
cout << endl;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
cout << map(i, j) << ' ';
}
cout << endl;
}
}
當它是一個註釋部分,如果我把下面的代碼行:
int aux;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
cin >> map(i, j);
}
}
或:
int aux;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
cin >> aux;
map(i, j) = aux;
}
}
或者甚至:
int aux;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
cin >> aux;
map(i, j) = i + j; // !!!
}
}
...它不起作用。但是,出於某種原因,以下內容會:
int aux;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
// cin >> aux;
map(i, j) = i + j; // Still the same thing as before, logically
}
}
我不明白。這裏發生了什麼?
編輯:對不起,忘了包括我的調試過程。
輸入:
3 5
1 2 3 4 5
1 2 3 4 5
第一線具有矩陣的尺寸。第二行和第三行是寫入它的值;但是,在第三行末尾按Enter之後,程序崩潰。
,它甚至沒有與該輸入工作:
3 5
1
2
3
4
5
1
2
3
4
5
其第二個5之後崩潰,再次。
你是什麼意思的「......它不會工作」? – 2013-03-02 18:55:46
@AndyProwl每個人都知道你應該*神奇*知道爲什麼它不會工作! – Rapptz 2013-03-02 18:56:57
@AndProwl謝謝你的注意。我會編輯我的帖子,以包括我遇到問題的方式。 – Mutoh 2013-03-02 18:56:57