0

Ok so I am trying to write a template that builds a 2D matrix, and I want the >> and << to work as normal, here is the code I have so far but I am lost. I have functions input and output to run a user through filling the template at the moment, so I want to be able to cin and cout the template.

#include <iostream>
#include <cstdlib>

using namespace std;

template <typename T  > 
class Matrix
{
    friend ostream &operator<<(ostream& os,const Matrix& mat);
    friend istream &operator>>(istream& is,const Matrix& mat);
    private:
        int R; // row
        int C; // column
        T *m;  // pointer to T
  public:
   T &operator()(int r, int c){ return m[r+c*R];}
   T &operator()(T a){for(int x=0;x<a.R;x++){
    for(int z=0;z<a.C;z++){
        m(x,z)=a(x,z);
    }   
   }
   }
   ~Matrix();
   Matrix(int R0, int C0){ R=R0; C=C0; m=new T[R*C]; }
   void input(){
       int temp;
       for(int x=0;x<m.R;x++){
           for(int y=0;y<m.C;y++){
               cout<<x<<","<<y<<"- ";
               cin>>temp;
               m(x,y)=temp;
           }
       }
   }
 };

// istream &operator>>(istream& is,const Matrix& mat){
//     is>>mat 
// };

ostream &operator<<(ostream& os,const Matrix& mat){
     for(int x=0;x<mat.R;x++){
         for(int y=0;y<mat.C;y++){
             cout<<"("<<x<<","<<y<<")"<<"="<<mat.operator ()(x,y);
         }

     }
 };

int main()
{
        Matrix<double> a(3,3);
        a.input();
        Matrix<double> b(a);
        cout<<b;

        cout << a(1,1);
}
sdla4ever
  • 128
  • 1
  • 15
  • Ok I'll add the void, I was using just the general pointer. I added the classes to the original post. – sdla4ever May 02 '13 at 01:38
  • What is the name of your class? – David G May 02 '13 at 01:39
  • class Matrix is what I'm using – sdla4ever May 02 '13 at 01:41
  • 1
    What do you mean by work as normal? There are [quite](http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/insertion.html) [a](http://msdn.microsoft.com/en-us/library/1z2f6c2k(v=vs.80).aspx) [few](http://www.parashift.com/c++-faq/output-operator.html) [tutiorals](http://stackoverflow.com/questions/2351972/whats-the-right-way-to-overload-the-stream-operators-for-my-class) on overloading these operators. I suggest consulting one as this looks nothing like the usual way of overloading the stream operators. – David Brown May 02 '13 at 01:43
  • Thanks @DavidBrown I will read those right now. I'm rusty as hell on overloading operators. – sdla4ever May 02 '13 at 01:51

1 Answers1

0

Here are all the problems I found with your code. Let's start from the beginning:

  • Wrong function return-type and assignment through this

    T operator>>(int c)
    {
        this = c;
    }
    

    Why is this code wrong? Well the first thing I notice is that your function is returning T yet you have no return statement present in the block. Forget about what I said in the comments, your insertion/exertion operators should return *this. It follows that your return-type should be Maxtrix&.

    Another error I see in this snippet is that you are assigning the this pointer. This shouldn't have compiled for you. Rather, if you meant to change a certain data member (preferably your C data member), it should have looked like this:

    this->C = c;
    

    In turn, this is what your function should look like:

    Matrix& operator>>(int c)
    {
        this->C = c;
        return *this;
    }
    
  • this->(z, x)

    In the inner for loop of your output function, you did this:

    cout << "(" << z << "," << x << ")" << "=" << this->(z, x) << endl;
    

    this->(z, x) isn't doing what you think. It doesn't concurrently access two of the matrix's data members. It will actually cause an error because of invalid syntax. You'll have to access the data members separately, like this:

    ... << this->z << this->x << endl;
    

    Moreover, this output function doesn't need a return-type. Just make it void.

    Note that you have the same problem in your input function.

David G
  • 94,763
  • 41
  • 167
  • 253
  • ok so I have changed the code to show everything I got after I read thru some of the links I was given above. – sdla4ever May 02 '13 at 20:38