More Issues with <<overload!

This is a discussion on More Issues with <<overload! within the C++ Programming forums, part of the General Programming Boards category; I have another problem... I have an overloaded << operator that won't work with external ofstream csis(csis is just a ...

  1. #1
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101

    More Issues with <<overload!

    I have another problem...

    I have an overloaded << operator that won't work with external ofstream csis(csis is just a text file)...the errror I"m getting is:

    c:\documents and settings\hp_administrator\my documents\visual studio 2005\projects\stringstuff\case.cpp(62) : error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'std:fstream' (or there is no acceptable conversion)

    my simple little function looks like this:

    Code:
    ostream& operator<<(ostream& os, String& str)
    {
    	os << str.getBuf() << endl;
    	return os;
    }
    where the declaration is:

    friend ostream& operator<<(ostream&, const String& str);


    Anyone?? Anyone??? I have not the slightest clue.... I hope that's enough information too::

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The parameter and/or return types in the declaration are different than in the definition. They must match.

  3. #3
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Ok I messed around and did the folllow and as such have no success (story of my life):

    Code:
    ostream& operator<<(std::ostream& os, String& str)
    {
    	os << str.getBuf() << endl;
    	return os;
    }
    
    ofstream& operator<<(std::ofstream& os, String& str)
    {
    	os << str.getBuf() << endl;
    	return os;
    }
    
    with a declaration as....................
    
    	void print();
    	//friend ostream& operator<<(ostream&, String& str);
    
    };
    
    std::ostream& operator <<(std::ostream& os, String& str);
    std::ofstream& operator<<(std::ofstream& os, String& str);
    #endif

    As usual I have not a clue....

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    definition is
    ostream& operator<<(ostream& os, String& str)

    declaration is
    friend ostream& operator<<(ostream&, const String& str);

    spot the difference?
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    See redefinition below:

    Code:
    ostream& operator<<(std::ostream& os, String& str)
    {
    	os << str.getBuf() << endl;
    	return os;
    }
    
    ofstream& operator<<(std::ofstream& os, String& str)
    {
    	os << str.getBuf() << endl;
    	return os;
    }
    
    with a declaration as....................
    
    	void print();
    	//friend ostream& operator<<(ostream&, String& str);
    
    };
    
    std::ostream& operator <<(std::ostream& os, String& str);
    std::ofstream& operator<<(std::ofstream& os, String& str);
    #endif

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,410
    Discard the ofstream& version. An ofstream is an ostream, so you only need the ostream one.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    even without the ofstream& I still get that error: error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'std:fstream' (or there is no acceptable conversion)
    Which I really don't know what to do with that.....

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Not sure if this will help but try placing the function inside namespace std.
    The cost of software maintenance increases with the square of the programmer's creativity.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,410
    Use an ofstream instead of an fstream. Do not place your free functions (or anything else aside from certain template specialisations, for that matter) in namespace std as that is not allowed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmap scroll issues
    By Gerread in forum Windows Programming
    Replies: 4
    Last Post: 05-14-2007, 05:18 AM
  2. Solution to culling issues?
    By VirtualAce in forum Game Programming
    Replies: 4
    Last Post: 03-14-2006, 05:59 PM
  3. SQL Connection issues
    By jverkoey in forum Tech Board
    Replies: 4
    Last Post: 02-17-2005, 06:52 AM
  4. Major game issues
    By VOX in forum Game Programming
    Replies: 0
    Last Post: 01-18-2005, 07:40 AM
  5. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 08:01 PM

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