473,854 Members | 1,684 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator overloads, ostream works, but not ofstream/ifstream

175 New Member
i'm working on a C++ program that uses operator overloads (which i'm just learning to use), and i have the ostream working fine, but i'm thinking i'm using it wrong somehow because the ofstream doesn't work. Here's some code fragments...

Expand|Select|Wrap|Line Numbers
  1. class matrix
  2. {
  3.  
  4. public:
  5.     ......
  6.  
  7.     friend ostream& operator << ( ostream& os, matrix& m );  
  8.     friend ifstream& operator >> ( ifstream& ifs, matrix& m );
  9.     friend ofstream& operator << ( ofstream& ofs, matrix& m );
  10.  
  11.     ......
  12.  
  13. private:
  14.  
  15.     ......
  16.  
  17. };
  18.  
  19. // in the class definition...
  20.  
  21. ostream& operator<<(ostream& os, matrix& m) 
  22. {
  23.                 .......
  24.     return os;
  25. }
  26.  
  27. // in the main...
  28.  
  29. matrix m;
  30. cout << m;
  31.  
  32.  
i took out most of the code, cause it's a LOT of code...i think this is enough to show that i know what i'm doing, but i THINK the problem is in main, where i'm trying to have the overloaded ostream and ofstream called.

as the code is now, the ostream works, but the ofstream does not work (neither does the ifstream). My question is, IF the problem is how i'm having the operator overloads called, what am i doing wrong?

(if anyone needs to see more code, i can post it, i just didn't want to put hundreds of lines of code up, because it didn't seem necessary)
Oct 8 '06 #1
4 4749
Banfa
9,065 Recognized Expert Moderator Expert
By "but the ofstream does not work" do you mean that it wont compile or it does give the correct results at runtime?

It it wont compile we need to know the compilation errors/warnings and the lines of code they occur on. If it gives the wrong output at run time we need to see the overload function code.
Oct 9 '06 #2
manontheedge
175 New Member
here's the code for the overloaded ostream operator...
( x is a member variable, an int, tells the size of the matrix)

Expand|Select|Wrap|Line Numbers
  1.  
  2. ostream& operator << ( ostream& os, matrix& m ) 
  3. {
  4.     os << "  ";    
  5.  
  6.     for ( int colNum = 0; colNum < m.x; colNum++)    
  7.     {                                os << colNum << " ";    
  8.     }                                
  9.     os << endl;
  10.  
  11.     for ( int rowNum = 0; rowNum < GP.x; rowNum++)            {                                os << rowNum << "|";    
  12.  
  13.         for ( int j = 0; j < m.x; j++)        
  14.            {                                os << m.matrix[rowNum][j] << " ";
  15.         }                                    
  16.         os << endl;                    }                                        
  17.     os << endl << endl;
  18.  
  19.     return os;
  20. }
  21.  
  22.  

here's the code for the overloaded ofstream operator...

Expand|Select|Wrap|Line Numbers
  1.  
  2. ofstream& operator << ( ofstream& ofs, matrix& m)
  3. {
  4.     ofstream out;
  5.     out.open( "test.txt" );                                
  6.     if ( out.is_open() )
  7.     {
  8.          for ( int i = 0; i < m.x; i++)                 
  9.                      {                    
  10.         os << rowNum << "|";    
  11.  
  12.         for ( int j = 0; j < m.x; j++)        
  13.            {                                os << m.matrix[i][j] << " ";
  14.         }                            
  15.                                 if ( j == m.x - 1 )
  16.                                 {
  17.                                   os << endl;                    }
  18.                      }                                        
  19.          os << endl << endl;
  20.  
  21.          return os;            
  22.          }
  23.  
  24.     cout << "...ofstream was called" << endl << endl;
  25.  
  26.     return ofs;
  27. }
  28.  
  29.  
...then, in main, i have...

Expand|Select|Wrap|Line Numbers
  1.  
  2. matrix m;
  3. cout << m;  // for the ostream and ofstream
  4.                   // the problem here is that, ONLY the ostream is called...
  5.                   // i also want the ofstream called, but is this wrong?
  6.  
  7. cin >> m;    // for the ifstream ...i get an error if this is included...says
  8.                   // "binary '>>' : no operator defined which takes a left-hand
  9.                   // operand   of type 'class istream_withassign' (or there is no
  10.                   // acceptable conversion)"
  11.  
  12.  

for the ifstream at this point, i'm more just trying to get it called. It will be reading what the ofstream puts in the file...i put in a cout just to see if it would be called, or if it was called, but in the main, it gives me that error, so i don't get a chance to see if it was called.

Expand|Select|Wrap|Line Numbers
  1.  
  2. ifstream& operator >> ( ifstream& ifs, matrix& m)
  3. {
  4.     ifstream myfile ( "test.txt" ); 
  5.  
  6.     if( !myfile.is_open() )
  7.     { 
  8.         cout << "there's a problem, file not found" << endl;
  9.     } 
  10.  
  11.     while( !myfile.eof() )
  12.     { 
  13. //           for ( int i = 0; i < size; i++ )
  14. //        {
  15.             myfile >> m.matrix[0][0]; //just seeing if it works
  16. //        }
  17.     }
  18.  
  19.     cout << "...ifstream caled" << endl;
  20.  
  21.     myfile.close();
  22.     return ifs;
  23. }
  24.  
  25.  
Oct 9 '06 #3
manontheedge
175 New Member
...sorry, the spacing is off in that last post, and i couldn't edit it,
so here it is again...


here's the code for the overloaded ostream operator...
( x is a member variable, an int, tells the size of the matrix)

Expand|Select|Wrap|Line Numbers
  1.  
  2. ostream& operator << ( ostream& os, matrix& m ) 
  3. {
  4.      os << "  ";    
  5.  
  6.      for ( int colNum = 0; colNum < m.x; colNum++)    
  7.      {                        
  8.         os << colNum << " ";    
  9.      }                                
  10.      os << endl;
  11.  
  12.      for ( int rowNum = 0; rowNum < GP.x; rowNum++)            
  13.      {                        
  14.         os << rowNum << "|";    
  15.  
  16.         for ( int j = 0; j < m.x; j++)        
  17.         {                        
  18.            os << m.matrix[rowNum][j] << " ";
  19.         }                        
  20.            os << endl;                    
  21.      }                             
  22.         os << endl << endl;
  23.  
  24.      return os;
  25. }
  26.  
  27.  

here's the code for the overloaded ofstream operator...

Expand|Select|Wrap|Line Numbers
  1.  
  2. ofstream& operator << ( ofstream& ofs, matrix& m)
  3. {
  4.      ofstream out;
  5.      out.open( "test.txt" );                                
  6.      if ( out.is_open() )
  7.      {  
  8.         for ( int i = 0; i < m.x; i++)                 
  9.         {                    
  10.            for ( int j = 0; j < m.x; j++)        
  11.            {                        
  12.               ofs << m.matrix[i][j] << " ";
  13.            }                            
  14.  
  15.            if ( j == m.x - 1 )
  16.            {
  17.               ofs << endl;                    
  18.            }
  19.         }                            
  20.  
  21.         cout << "...ofstream was called" << endl << endl;
  22.  
  23.         return ofs;
  24.      }
  25. }
  26.  
  27.  
...then, in main, i have...

Expand|Select|Wrap|Line Numbers
  1.  
  2. matrix m;
  3. cout << m;  // for the ostream and ofstream
  4.                   // the problem here is that, ONLY 
  5.                   // the ostream is called...
  6.                   // i also want the ofstream called, 
  7.                   // but is this wrong?
  8.  
  9. cin >> m;    // for the ifstream ...i get an error 
  10.                   // if this is included...says
  11.                   // "binary '>>' : no operator defined 
  12.                   // which takes a left-hand
  13.                   // operand   of type 'class istream_withassign' 
  14.                   // (or there is no
  15.                   // acceptable conversion)"
  16.  
  17.  

for the ifstream at this point, i'm more just trying to get it called.
It will be reading what the ofstream puts in the file...i put in a cout
just to see if it would be called, or if it was called, but in the main,
it gives me that error, so i don't get a chance to see if it was called.

Expand|Select|Wrap|Line Numbers
  1.  
  2. ifstream& operator >> ( ifstream& ifs, matrix& m)
  3. {
  4.      ifstream myfile ( "test.txt" ); 
  5.  
  6.      if( !myfile.is_open() )
  7.      { 
  8.         cout << "there's a problem, file not found" << endl;
  9.      } 
  10.  
  11.      while( !myfile.eof() )
  12.      { 
  13. //      for ( int i = 0; i < size; i++ )
  14. //      {
  15.            myfile >> m.matrix[0][0]; //just seeing if it works
  16. //      }
  17.      }
  18.  
  19.      cout << "...ifstream caled" << endl;
  20.  
  21.      myfile.close();
  22.      return ifs;
  23. }
  24.  
  25.  
Oct 9 '06 #4
manontheedge
175 New Member
i figured out the problem, and everything works perfectly now. I noticed several people have looked at this...if anyone is curious i can put up the corrections...i t didn't take much.
Oct 9 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
8097
by: KK | last post by:
Hi, im working on this bigInt class. Need help writing algorithm for the operator*, andy help will be appreciated. Thanks in advance bigInt.h =================================================================================== //class bigInt implements big integers by storing the digits of a big //integer in a private vector data member called digit. Since it uses a //vector the size of the big integer can be indefinitely large. This...
11
2299
by: David Briggs | last post by:
I am using MFC VC++ 6.0 and I have a simple class 'Data' with a CString member var. Name. I added operator << and >> as: #include <istream> #include <ostream> #include <fstream> std::ostream& operator<<(std::ostream& fs, const Data& x ) {
2
5689
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have seen commercial programs print output to the screen as well as to a log file. depending on the user and other situations, i might want to turn off one of the outputs or maybe even both outputs. so, i want a single line with operator << function...
6
12557
by: c++newbie | last post by:
Hi all, I try to compile the following classes: main.cpp: #include <algorithm> #include <iostream> #include <fstream> #include <iterator>
3
2964
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD FUNCTION!!
20
3880
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in a named namespace namespace foo { class Foo
2
2186
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const Debug_Level ddl;
2
2468
by: tvn007 | last post by:
Please help why this code not compile I would like it to be something like this: istringstream stringstreams(line); stringstreams>>ptrstudent->bypassed>>ptrstudent->name>>ptrstudent->midterm; stringstreams>>ptrstudent->quiz>>ptrstudent->final>>ptrstudent->testname; /////////////////////////////////////////////////////////////////////////////////////////////////////// #include <iostream> #include <string> #include <fstream>
6
10060
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he hoped I could solve, and I couldn't. Some code he's using, written in 1999, that compiled fine in 1999, no longer does in 2006 with g++ 4. This little bit of code: SimS::SimS (ostream &s) {
0
9901
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11024
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10672
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10744
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7909
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5937
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.