Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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;
 
}
Posted 21-May-13 21:03pm
RHsoul376
Edited 21-May-13 23:43pm
v5

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Quote:
Mtrx & operator ++ ( int );
Quote:
template
Mtrx & Mtrx :: operator ++ (int)
{

Mtrx 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
Mtrx  operator ++ ( int );
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;
 
}
  Permalink  
Comments
RHsoul at 22-May-13 5:45am
   
Thanks a lot!
CPallini at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 22 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100