0

I have my assignment and need help very badly - would you be kind enough to answer the bellow question?

1 - Convert the Matrix class to a template class. Test the matrix template with a suitable main function that declares a matrix of complex numbers, inputs from keyboard/file and outputs to screen/file.

So the original code follows as

#ifndef MATRIX_H  //include guard
#define MATRIX_H

#include <iostream>  //generic IO
#include <fstream>   //file IO
#include <stdexcept> //provides exceptions
#include "vector.h"  //we use Vector in Matrix implementation

class Matrix {
private:
    Vector v;     // Vector used to store the matrix elements
    int nrows;    // number of rows of the matrix
    int ncols;    // number of columns of the matrix

public:
    // CONSTRUCTORS
    Matrix(); // default constructor, uses default constructor for v
    Matrix(int Nrows, int Ncols);  // alternate constructor
    Matrix(const Vector& v); // build a matrix from a vector
    Matrix(const Matrix& m); // copy constructor

    // ACCESSOR METHODS
    int getNrows() const; // get the number of rows
    int getNcols() const; // get the number of cols

    // OVERLOADED FUNCTION CALL OPERATORS
    double& operator() (int i, int j); //  function call overload (-,-) for assignment
    double operator() (int i, int j) const; // for reading matrix values
    Matrix& operator=(const Matrix& m); // overloaded assignment operator
    bool operator==(const Matrix& m) const; // overloaded comparison operator

    // INPUT & OUTPUT 
    friend std::istream& operator>>(std::istream& is, Matrix& m);// keyboard input
    friend std::ostream& operator<<(std::ostream& os, const Matrix& m);// screen output

    //the file output operator is compatible with file input operator,
    //ie. everything written can be read later.
    friend std::ifstream& operator>>(std::ifstream& ifs, Matrix& m);// file input
    friend std::ofstream& operator<<(std::ofstream& ofs, const Matrix& m);// file output
};

// Note: There is no strict need for a copy constructor or 
// assignment operator in the Matrix class since the 
// compiler versions of these methods will work OK. It will copy/assign 
// the values of nrows and ncols and call the Vector copy 
// constructor/assignment operator automatically for the 
// Vector part v inside the Matrix. However they are written
// for clarity and understanding

#endif

In order to convert it to a template I changed the features with template

#ifndef MATRIX_H  //include guard
#define MATRIX_H

#include <iostream>  //generic IO
#include <fstream>   //file IO
#include <stdexcept> //provides exceptions
#include "vector.h"  //we use Vector in Matrix implementation

class Matrix {
    template <typename T> class T
protected:
    Vector v;     // Vector used to store the matrix elements
    int num; // Number of elements
    int nrows;    // number of rows of the matrix
    int ncols;    // number of columns of the matrix
    T* pdata; // Pointer to the data
    void Init(int Num); // private function since user should not call it

public:
    // CONSTRUCTORS
    Matrix(); // default constructor, uses default constructor for v
    Matrix(int Nrows, int Ncols);  // alternate constructor
    Matrix(const Matrix& v); // build a matrix from a vector
    Matrix(const Matrix& m); // copy constructor

    // SIZE
    int size() const; //get number of elements in the vector

    // ACCESSOR METHODS
    int getNrows() const; // get the number of rows
    int getNcols() const; // get the number of cols

    // OVERLOADED FUNCTION CALL OPERATORS
    Matrix<T>& operator() (int i, int j); //  function call overload (-,-) for assignment
    T& operator() (int i, int j) const; // for reading matrix values
    T operator[] (int i) const; // overloaded array access operator for reading
    Matrix& operator=(const Matrix& m); // overloaded assignment operator
    bool operator==(const Matrix& m) const; // overloaded comparison operator

    // INPUT & OUTPUT 
    friend std::istream& operator>>(std::istream& is, Matrix& m);// keyboard input
    friend std::ostream& operator<<(std::ostream& os, const Matrix& m);// screen output

    //the file output operator is compatible with file input operator,
    //ie. everything written can be read later.
    friend std::ifstream& operator>>(std::ifstream& ifs, Matrix& m);// file input
    friend std::ofstream& operator<<(std::ofstream& ofs, const Matrix& m);// file output
};

// Note: There is no strict need for a copy constructor or 
// assignment operator in the Matrix class since the 
// compiler versions of these methods will work OK. It will copy/assign 
// the values of nrows and ncols and call the Vector copy 
// constructor/assignment operator automatically for the 
// Vector part v inside the Matrix. However they are written
// for clarity and understanding

#endif

and finally to test the Matrix I entered at a separate class

   #include <iostream>
#include <cmath>
#include <vector.h>
#include <complex.h>
#include <matrix.h>


Matrix<Complex> matrix_vector(10);

    std::cin >> matrix_vector;

Please please, I'm getting errors and not entirely sure if I am correct at all places. I would highly appreciate. Thanks so much

Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
  • 1
    What Errors ? Did you read [Why can templates only be implemented in the header file?](http://stackoverflow.com/q/495021/1870232) – P0W Nov 25 '13 at 17:24
  • 2
    One obvious error is the syntax and placement of the line: `template class T`. This should come right *before* the `class Matrix {` line and you don't need the `class T` part. You should look at any examples you've been given about this. – Turix Nov 25 '13 at 17:31
  • If the hint from @Turix doesn't get you moving far, look at the very first error message and try to fix just that, then repeat. If you don't understand an error message, you can ask a question about it here, making sure to post the entire error message, the line it points at, and any other code you think might be relevant. – aschepler Nov 25 '13 at 17:34
  • Remember, the data type used in your vector class must match the data item for Matrix. – Thomas Matthews Nov 25 '13 at 18:48

0 Answers0