the operator+ and operator^ are not working. Can anyone correct?.
Code:#include <iostream> #include <algorithm> using namespace std; template <class T> class Array2D { T* data; int w, h; public: Array2D(int X, int Y) : w(X), h(Y) { data = new T[w*h]; } ~Array2D() { if (data != NULL) { delete [] data; data = NULL; } } T& operator ()(int Col, int Row) { return data[Row*w+Col]; } Array2D operator+( Array2D& m) { int w = m.width(); int h = m.height(); Array2D theArray2D(w,h); for (int r=0;r<h;r++) { for (int c=0;c<w;c++) { theArray2D(c,r)=data[c*w+r]+m(c,r); } } return theArray2D; } Array2D operator^( cont T& t) { //need to be filled } int width() {return w;} int height() {return h;} }; int main( ) { int c = 5; int r = 6; Array2D<int> b(c,r); Array2D<int> b1(c,r); for(int j=0; j<r; j++) { for(int i=0; i<c; i++) { b(i,j) = i+j; b1(i,j) = i+j; } } Array2D<int> a1 = b + b1; Array2D<int> a2 = b ^ 2; for(int j=0; j<r; j++) { for(int i=0; i<c; i++) { std::cout << a1(i,j) << "\t"; } std::cout << "\n"; } return 0; }




Reply With Quote