Pointer Fehler?
-
Ich bin dran ne matrixklasse zu schrieben in MSVC 08. Die Klasse kompiliert fehlerfrei. Wenn ich die konsole jedoch starte um das ganze zu testen erhalte ich folgende fehlermeldung:
'matrix.exe': Loaded 'C:\Documents and Settings\clypto89\My Documents\Visual Studio 2008\Projects\matrix\Debug\matrix.exe', Symbols loaded.
'matrix.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
'matrix.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
'matrix.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcp90d.dll', Symbols loaded.
'matrix.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcr90d.dll', Symbols loaded.
First-chance exception at 0x102d31ea (msvcr90d.dll) in matrix.exe: 0xC0000005: Access violation reading location 0xfeeefee2.
Unhandled exception at 0x102d31ea (msvcr90d.dll) in matrix.exe: 0xC0000005: Access violation reading location 0xfeeefee2.#include <iostream> #include <math.h> using namespace std; class matrix { public: matrix(int n); matrix(int rows=0, int cols=0); ~matrix(); int matrix::getR(); int matrix::getC(); double** matrix::getE(); double* matrix::getR(int); double* matrix::getC(int); double& matrix::operator()(int, int); double matrix::operator() (int, int) const; friend ostream& operator<<(ostream&, const matrix&); friend istream& operator>>(istream& , matrix&); matrix matrix::zeros(int,int); matrix matrix::identity(int,int); friend bool operator==(double&,const matrix&,const matix&); friend matrix operator+(const matrix&,const matrix&); friend matrix operator-(const matrix&,const matrix&); friend matrix operator*(const matrix&,const matrix&); friend matrix operator-(matrix&); friend matrix& operator+=(matrix&,const matrix&); friend matrix& operator-=(matrix&,const matrix&); friend matrix& operator*=(matrix&,const matrix&); matrix& matrix::permutation(int,int,matrix&); matrix& matrix::transposition(int,double,matrix&); matrix& matrix::rowaddition(int,int,double,matrix&); private: int rows; int cols; double** element; }; matrix::matrix(int n) { rows=n; cols=n; element=NULL; element = new double *[rows]; for(int i =0; i < rows; ++i) { element[i]=new double[cols]; } } matrix::matrix(int m, int n) { rows=m; cols=n; element=NULL; element = new double *[rows]; for(int i =0; i < rows; ++i) element[i]=new double[cols]; } matrix::~matrix() { for (int i = 0; i < rows ; ++i) { delete [] elements[i]; } delete [] elements; } int matrix::getR() { return rows; } int matrix::getC() { return cols; } double** matrix::getE() { return element; } double& matrix::operator() (int m, int n) { return element[m-1][n-1]; } double matrix::operator() (int m, int n) const { return element[m-1][n-1]; } ostream& operator<<(ostream & result, const matrix& A) { for(int i = 0; i < A.rows; ++i) { cout<<endl; for(int j=0; j < A.cols; j++) result << A.element[i][j]<<"\t"; } cout<<endl; return result; } istream& operator>> (std::istream& in, matrix& A) { for(int i = 0; i < A.rows; ++i) { for(int j = 0; j < A.cols; ++j) { std::cout << "a[" << i+1 << "][" << j+1 << "]="; in >> A.element[i][j]; } } return in; } matrix matrix::zeros(int m, int n) { matrix A(m,n); for(int i = 0; i < m; ++i) { for(int j = 0; j < n; ++j) { A.element[i][j]=0; } } return A; } matrix matrix::identity(int m, int n) { matrix A(m,n); for(int i = 0; i < m; ++i) { for(int j = 0; j < n; ++j) { if(i != j) { A.element[i][j] = 0; } else { A.element[i][j] = 1; } } } return A; } bool operator==(double& precision, const matrix& A, const matix& B) { precision = fabs(precision); for(int i = 0; i < A.rows; ++i) { for(int j = 0; j < A.cols; ++j) { if(fabs( A.element[i][j]-B.element[i][j] )>precision) { return false; } } } return true; } matrix operator+(const matrix& A, const matrix& B) { matrix C(A.rows,A.cols); for(int i = 0; i < A.rows; ++i) for(int j = 0; j < A.cols; ++j) C.element[i][j]=A.element[i][j]+B.element[i][j]; return C; } matrix operator-(const matrix& A, const matrix& B) { matrix C(A.rows,A.cols); for(int i = 0; i < A.rows; ++i) for(int j = 0; j < A.cols; ++j) C.element[i][j]=A.element[i][j]-B.element[i][j]; return C; } matrix operator*(const matrix& A, const matrix& B) { double sum = 0; matrix C(A.rows,B.cols); for(int i = 0; i < A.rows; ++i) { for(int j = 0; j < B.cols; ++j) { for(int k = 0; k < A.cols; ++k) { sum +=A.element[i][k]*B.element[k][j]; } C.element[i][j] = sum; sum = 0; } } return C; } matrix operator-(matrix& A) { for(int i = 0; i < A.rows; ++i) for(int j = 0; j < A.cols; ++j) A.element[i][j]=-A.element[i][j]; return A; } matrix& operator+=(matrix& A,const matrix& B) { for(int i = 0; i < A.rows; ++i) for(int j = 0; j < A.cols; ++j) A.element[i][j]+=B.element[i][j]; return A; } matrix& operator-=(matrix& A,const matrix& B) { for(int i = 0; i < A.rows; ++i) for(int j = 0; j < A.cols; ++j) A.element[i][j]-=B.element[i][j]; return A; } matrix& operator*=(matrix& A,const matrix& B) { double sum = 0; matrix C(A.rows,B.cols); for(int i = 0; i < A.rows; ++i) { for(int j = 0; j < B.cols; ++j) { for(int k = 0; k < A.cols; ++k) { sum +=A.element[i][k]*B.element[k][j]; } A.element[i][j] = sum; sum = 0; } } return A; } matrix& matrix::permutation(int i,int j,matrix& A) { double store; int k = 0; for(; k < A.cols; ++k) store = A.element[j][k]; A.element[j][k]=A.element[i][k]; A.element[i][k]=store; return A; } matrix& matrix::transposition(int m, double y, matrix& A) { for(int k = 0; k < A.cols; ++k) A.element[m][k]+=y*A.element[m][k]; return A; } matrix& matrix::rowaddition(int from,int to,double y,matrix& A) { for(int k = 0; k < A.cols; ++k) A.element[to][k]+=y*A.element[from][k]; return A; } int main() { int n; cout<<"enter the size of the matrix:"; cin>>n; matrix A(n,n); matrix B(n,n); matrix C(n,n); cin>>A; cin>>B; C=A+B; cout<<A; cout<<B; cout<<C; C=A-B; cout<<C; C=A*B; cout<<C; cin>>n; return 0; };
mein gefühl sagt mir das irgendwas mit nem pointer nicht in ordnung ist aber ich finde den fehler nicht. Ich wäre sehr dankbar um hilfe.
mit freundlichen grüssen.
hans
-
First-chance exception at 0x102d31ea (msvcr90d.dll) in matrix.exe: 0xC0000005:
Access violation reading location 0xfeeefee2Da wird der debugger bestimmt auch n break vorgeschlagen habe,...
a) zeige mal die stelle wo
b) meine vermutung: uninitialisierter ptrgrüüüße