Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overloading operator >> to read a text file into a Matrix

Reply
Thread Tools

overloading operator >> to read a text file into a Matrix

 
 
bluekite2000@gmail.com
Guest
Posts: n/a
 
      09-14-2005
I have
template <typename T>
inline std::istream& operator >> (std::istream& is, Matrix<T>& mIn)
{
int rows,cols;
is.clear();
is >> rows >> cols;
mIn._M=rows;
mIn._N=cols;
mIn.MatAlloc(rows,cols);//alllocate mem

for (int i=0;i<rows;i++)
for (int j=0;j<cols; j++)
if(is.good())
is >> mIn(i,j);

return is;
}
This works fine if i have a text file such as
2 3 //2 rows 3 cols
1 2 3
4 5 6

but if i have something like
2 3
1 2


I get a matrix M with the following data member
2 3
1 2 2
0 0 0

notice how M(0,1)==M(0,2) ==2 which i DONT want. Any idea?

 
Reply With Quote
 
 
 
 
=?iso-8859-1?Q?Ali_=C7ehreli?=
Guest
Posts: n/a
 
      09-14-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>I have
> template <typename T>
> inline std::istream& operator >> (std::istream& is, Matrix<T>& mIn)
> {
> int rows,cols;
> is.clear();
> is >> rows >> cols;
> mIn._M=rows;
> mIn._N=cols;
> mIn.MatAlloc(rows,cols);//alllocate mem
>
> for (int i=0;i<rows;i++)
> for (int j=0;j<cols; j++)
> if(is.good())
> is >> mIn(i,j);


is will be "not good" only after attempting to read. Try this:

for (int i=0;i<rows;i++)
for (int j=0;j<cols; j++)
{
int value;
if (is >> value)
{
mIn(i, j) = value;
}
}

Ali

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(x,y,z) Operator overloading on matrix Jaco Naude C++ 1 01-14-2010 09:51 AM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
overloading [] operator for matrix? Dave C++ 3 02-28-2004 07:22 AM



Advertisments