I get a wrong answer with a++.
I input a:
1 2 3
4 5 6
7 8 9
It shows " this is a -58993460 * -58993460 matrix.
I don't konw why.
Who can help me ?
#include<iostream>
using namespace std;
template < typename Type >
class Mtrx
{
private:
int m_iRow , m_iColumn;
Type ** yourmtrx;
public:
Mtrx();
Mtrx(int x , int y);
~Mtrx();
Mtrx ( const Mtrx & m );
Mtrx & operator = ( const Mtrx & m );
Mtrx & operator ++ ();
Mtrx & operator ++ ( int );
template<typename type="">
friend std::ostream & operator << ( std::ostream & os , const Mtrx & m );
template<typename type="">
friend std::istream & operator >> ( std::istream & is , Mtrx & m );
};
template < typename Type >
Mtrx < Type > :: Mtrx()
{
m_iRow=3;
m_iColumn=3;
yourmtrx = new Type* [3];
for (int i=0; i<3; i++)
yourmtrx[i] = new Type [3];
}
template <typename type="">
Mtrx <type> :: Mtrx (int x , int y)
{
m_iRow=x;
m_iColumn=y;
yourmtrx = new Type* [m_iRow];
for (int i=0; i<m_irow;> yourmtrx[i] = new Type [m_iColumn];
}
template <typename type="">
Mtrx <type> :: Mtrx (const Mtrx<type> & m)
{
m_iRow=m.m_iRow;
m_iColumn=m.m_iColumn;
yourmtrx = new Type* [m_iRow];
for (int i=0; i<m_irow;> yourmtrx[i] = new Type [m_iColumn];
for(int i=0; i<m_irow;> for(int k=0; k<m_icolumn;> yourmtrx[i][k] = m.yourmtrx[i][k];
}
template <typename type="">
Mtrx <type> :: ~Mtrx ()
{
delete [] yourmtrx;
}
template <typename type="">
Mtrx<type> & Mtrx <type> :: operator = (const Mtrx<type> & m)
{
if (this == &m)
return * this;
if(m_iRow!=m.m_iRow || m_iColumn!=m.m_iColumn)
{
delete [] yourmtrx;
m_iRow=m.m_iRow;
m_iColumn=m.m_iColumn;
yourmtrx = new Type* [m_iRow];
for (int i=0; i<m_irow;> yourmtrx[i] = new Type [m_iColumn];
}
for(int i=0; i<m_irow;> for(int k=0; k<m_icolumn;> yourmtrx[i][k] = m.yourmtrx[i][k];
return * this;
}
template <typename type="">
Mtrx <type> & Mtrx <type> :: operator ++ ()
{
for(int i=0; i<m_irow;> for(int j=0; j<m_icolumn;> yourmtrx[i][j]++;
return * this;
}
template <typename type="">
Mtrx <type> & Mtrx <type> :: operator ++ (int)
{
Mtrx <type> last = *this;
for (int i=0; i<m_irow;> for (int j=0; j<m_icolumn;> yourmtrx[i][j]++;
return last;
}
template <typename type="">
std::ostream & operator << (std::ostream & os , const Mtrx <type> & m)
{
os << " this is a " << m.m_iRow << " * " << m.m_iColumn << " matrix." << endl;
for (int i=0; i<m.m_irow;> {
for (int j=0; j<m.m_icolumn;> os << m.yourmtrx[i][j] << " ";
os << endl;
}
return os;
}
template <typename type="">
std::istream & operator >> (std::istream & is , Mtrx<type> & m)
{
for (int i=0; i<m.m_irow;> for (int j=0; j<m.m_icolumn;> {
is >> m.yourmtrx[i][j];
if(!is)
cerr<<"input error!"<<endl;
}
return is;
}
int main()
{
Mtrx<int> a;
cin>>a;
cout<<a++;
cout<<++a;
}