use <fstream> and ostream overloading together

This is a discussion on use <fstream> and ostream overloading together within the C++ Programming forums, part of the General Programming Boards category; Hy all, I create a small project that dealls with some student data structure. Now I want this program to ...

  1. #1
    Registered User mosu''s Avatar
    Join Date
    Jul 2006
    Location
    Cluj Napoca - Romania
    Posts
    10

    use <fstream> and ostream overloading together

    Hy all,

    I create a small project that dealls with some student data structure. Now I want this program to read the data from a file and write it to another.
    I think that using <fstream> library is the most easy way. But I also have in my program the ostream operator overloaded and I don't really succeeded to make them work togheter.

    This is what I tried:
    Code:
    #include <iostream.h>
    #include <fstream>
    #include <string>
    
    using std::ifstream;
    using std::string;
    using std::getline;
    Is silly but I'm in a hurry. This does not work because the "cout" is not overloaded to write a string line ...
    Any ideea will be greatly apreciated.
    Thank you!

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I don't understand your question.
    but using
    Code:
    #include <iostream>
    might help
    Kurt

  3. #3
    Registered User mosu''s Avatar
    Join Date
    Jul 2006
    Location
    Cluj Napoca - Romania
    Posts
    10
    Quote Originally Posted by ZuK
    I don't understand your question.
    but using
    Code:
    #include <iostream>
    might help
    Kurt

    well, I solved the problem ...
    I was mixing <iostream> with <iostream.h>.

    But I have now a curiosity:
    Why I can't write:
    friend ostream& operator <<(ostream& os, student& st);
    and use it without having to use the "std" namespce ?
    If I only include <iostream> it gives me a lot of errors including: "missing ';' before '&' " ...
    If I also put "using namspace std" everything is ok.
    Any ideea ?
    Thank you!

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Because ostream is in the std namespace. It worked with <iostream.h> because that is an old, non-standard header that didn't use namespaces. When you switch to the standard header <iostream>, you have to specify the std namespace just like with <fstream> and <string>.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overloading <<
    By msshapira in forum C++ Programming
    Replies: 2
    Last Post: 05-06-2009, 02:11 PM
  2. Help with Operator Overloading
    By Bnchs in forum C++ Programming
    Replies: 2
    Last Post: 11-29-2006, 11:26 AM
  3. problems overloading outstream for custom ADT
    By aciarlillo in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2005, 09:28 AM
  4. issues using ostream while overloading []
    By Mr_Jack in forum C++ Programming
    Replies: 1
    Last Post: 01-12-2004, 11:00 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21