重载“<<<<"”的问题有什么问题? [英] What's wrong with overloading of "<<"?

查看:161
本文介绍了重载“<<<<"”的问题有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用++给出了错误的答案。

我输入了:

1 2 3

4 5 6

7 8 9

显示这是-58993460 * -58993460矩阵。

我不知道为什么。

谁可以帮助我?





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;

}

推荐答案

引用:

Mtrx& operator ++(int);

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] ++;

最后返回;



}

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;

}





这些都是错误的:postincrement运算符应返回按值其结果,以这种方式更改它们



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;

}


这篇关于重载“&lt;&lt;&lt;&lt;&quot;”的问题有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆