same template class as in the other thread, but i'll post the current prototypes first for orientation:
The 'at' function is my current problem. I'd obviously like to have a prototype of the formCode:template<typename T, int n> // n is the Matrix height/width class Matrix { private: T** content; // 2-dimensional array of T objects public: // default constructor should use default T object if there is one Matrix(); // creates uniform Matrix (all values = t) Matrix(const T& t); // construct a Matrix from an array Matrix(const T arr[][n]); // copy constructor Matrix(const Matrix& m); // destructor ~Matrix(); // assignment Matrix<T, n>& operator=(const Matrix<T, n>& m); // 'at' function (indexing) T at(const int i, const int j); // basic matrix operations // addition Matrix<T, n> operator+(const Matrix<T, n>& m) const; // scalar multiplication friend Matrix<T, n> operator*(T t, const Matrix<T, n>& m); //Matrix<T, n> scalar_mult(const T& t) const; // other methods void show(); };
T& at(const int i, const int j);
but I can't get it to work that way. Here's the definition:
Aside from whether or not the else part will even work, this stops working as soon as I change the return type to T& and then in the if part use return &content[i - 1][j - 1]; (I thought it was kind of nice to convert to standard matrix notation.Code:template<typename T, int n> T Matrix<T, n>::at(const int i, const int j) { if (1 <= i && i <= n && 1 <= j && j <= n) return content[i - 1][j - 1]; else { std::cout << "Invalid index!\n"; return NULL; } }
How do I get a reference here so that I can not only show what the value is but also use this function to assign values inside my matrix?



LinkBack URL
About LinkBacks


