Click here to Skip to main content
15,881,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ?


C++
#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;

}
Posted
Updated 21-May-13 22:43pm
v5

1 solution

Quote:
Mtrx & operator ++ ( int );
Quote:
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;

}


Those are wrong: the postincrement operator should return by value its result, change them this way
C++
Mtrx  operator ++ ( int );

C++
template <typename type>
Mtrx <type>  Mtrx <type> :: operator ++ (int)
{

    Mtrx <type> last = *this;
    for (int i=0; i<m_iRow;i++)
        for (int j=0; j<m_iColumn;j++)
            yourmtrx[i][j]++;
    return last;

}
 
Share this answer
 
Comments
RHsoul 22-May-13 5:45am    
Thanks a lot!
CPallini 22-May-13 5:53am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900