CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [beginner]streams, operator >> overloading troubles

  1. #1
    Join Date
    Jan 2011
    Posts
    101

    [beginner]streams, operator >> overloading troubles

    Ok, I'll first layout code and then ask question:
    String.h
    Code:
    #ifndef String_HEADER_INCLUDED
    #define String_HEADER_INCLUDED
    
    #include <iostream>
    #include <sstream>
    
    namespace testNamespace
    {
    
    	class String
    	{
    	public:
    		std::stringstream sstr;
    
    		friend std::ostream& operator << (std::ostream& os,const String& obj)
    		{
    			os << obj.sstr.str();
    			return os;
    		}
    
    		friend std::istream& operator >> (std::istream& is, const String& obj)
    		{
    			is >> obj.sstr.str();// HERE LIES MY PROBLEM
    			return is;
    		}
    	};
    };
    Main.cpp
    Code:
    #include <fstream>
    #include "String.h"
    
    int main()
    {
    	testNamespace::String outStr;
    	outStr.sstr << "This is some test text to use!" << std::endl;
    
    	std::string fileName = "test.txt";
    
            //HERE IS COMMENTED PART WHERE
            // i WAS PREVIOUSLLY WRITTEN TEXT TO A FILE
    	//std::fstream outStream(fileName, std::ios::out);
    	//outStream << outStr << std::endl;
    	//outStream.close();
    
    	testNamespace::String inStr;
    	std::ifstream inStream(fileName, std::ios::in);
    	inStream >> inStr;
    	inStream.close();
    
    	std::cout << "TEXT FROM FILE :\t" << inStr << std::endl;
    
    	std::cin.get();
    	return 0;
    }
    Its simple console project to learn some STL functionality and operator overloading.
    I have outputted file with proper name and content but can't get content
    back in from file into my string object, nothing is displayed in console.
    Can you look in "String.h" and tell me how should i properly add this operator to work?
    Where i am making mistake?
    If you need additional info just tell me.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,716

    Re: [beginner]streams, operator >> overloading troubles

    Code:
    is >> obj.sstr.str();// HERE LIES MY PROBLEM
    I think that stringstream::str() returns a copy of the internal
    string, not a reference.

    I would suggest using a std::string as the member variable instead
    of a stringstream.

    I think that strinstream objects are not copyable, so you could
    not make copies of String objects if you keep a stringstream
    as a member variable.

  3. #3
    Join Date
    Jan 2011
    Posts
    101

    Re: [beginner]streams, operator >> overloading troubles

    I was using std::string before, but it doesnt have operator = + += etc. for primitive number types so i added it in my custom String class by using:
    Code:
    ...
    String& String::operator = (int inum)
    {
    	char buffer[MAX_CHARS_IN_INUM];
    	errno_t err = _itoa_s(inum, buffer, MAX_CHARS_IN_INUM, 10);
    	if(0 == err)
    	{
    		str = buffer;//where str is object of std::string type
    	}
    	return *this;
    }
    But later, when i heard about std::stringstream that it has that ability:
    Code:
    int someNumber = 50;
    std::stringstream str;
    str << "Number is " << someNumber;
    i decided to use this instead of std::string. I was hoping for some simple solution to this by not adding some buffer to pass to string, its redundant.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [beginner]streams, operator >> overloading troubles

    Quote Originally Posted by borko1980 View Post
    I was using std::string before, but it doesnt have operator = + += etc. for primitive number types so i added it in my custom String class by using:
    Code:
    ...
    String& String::operator = (int inum)
    {
    	char buffer[MAX_CHARS_IN_INUM];
    	errno_t err = _itoa_s(inum, buffer, MAX_CHARS_IN_INUM, 10);
    	if(0 == err)
    	{
    		str = buffer;//where str is object of std::string type
    	}
    	return *this;
    }
    But later, when i heard about std::stringstream that it has that ability:
    Code:
    int someNumber = 50;
    std::stringstream str;
    str << "Number is " << someNumber;
    i decided to use this instead of std::string. I was hoping for some simple solution to this by not adding some buffer to pass to string, its redundant.
    But as stated, streams are not copyable. Making a stream a member of a class produces undefined behaviour (if the code does compile) if a copy is made.

    Second, use std::ostringstream, not stringstream.

    Third, why not just write a toString() template function?
    Code:
    #include <sstream>
    #include <string>
    #include <iostream>
    
    template <typename T>
    std::string toString(T val)
    {
        std::ostringstream strm;
        strm << val;
        return strm.str();
    }
    
    int main()
    {
       std::string s = toString(3) + " " + toString(4.3);
       std::cout << s;
    }
    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jan 2011
    Posts
    101

    Re: [beginner]streams, operator >> overloading troubles

    Thanks. But i was hoping to not include some temporary variables, but to write directly to it.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: [beginner]streams, operator >> overloading troubles

    You could use boost::lexical_cast, but this is more or less equivalent to the template function Paul mentions.

    Don't get too worked up about the excess string copying-----much of it will go away when you compile optimized anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)