Click here to Skip to main content
15,885,887 members

What's wrong with overloading of "<<"?

RHsoul asked:

Open original thread
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;

}
Tags: C++

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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