Thread: More Issues with <<overload!

  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,366
    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
    28,413
    Discard the ofstream& version. An ofstream is an ostream, so you only need the ostream one.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 06:59 PM
  3. SQL Connection issues
    By jverkoey in forum Tech Board
    Replies: 4
    Last Post: 02-17-2005, 07:52 AM
  4. Major game issues
    By VOX in forum Game Programming
    Replies: 0
    Last Post: 01-18-2005, 08:40 AM
  5. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 09:01 PM