좋아요, 저는 동적 인 2 차원 행렬 클래스를 구현하고 있습니다.어떤 방식 으로든 Functor 클래스를 읽을 수 없습니다.
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) 후 충돌합니다.
다음Type& operator() (int i, int j) {
return mat[j * columns + i];
};
, i
명확하게 열이며 j
은 행입니다 :
"... 작동하지 않음"이란 무엇을 의미합니까? –
@AndyProwl 모두가 왜 마법이 작동하지 않는지를 알고 있다고 생각합니다! – Rapptz
@AndyProwl 고맙습니다. 내가 정확하게 문제가 발생하는 방법을 포함하도록 게시물을 편집 할 것입니다. – Mutoh