stream / bitshift overloads

This is a discussion on stream / bitshift overloads within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble doing some overloads. Be prepared for a lot of code: Code: template <ostream> ostream& operator <<(ostream & ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    stream / bitshift overloads

    I'm having trouble doing some overloads.

    Be prepared for a lot of code:

    Code:
    	template <ostream>
    	ostream& operator <<(ostream & os, const any & operand)
    	//postcondition: operand is written to output stream os
    	{
    		if (operand.type() == typeid(int))
    			return os << any_cast<int>(operand);
    		else if (operand.type() == typeid(char))
    			return os << any_cast<char>(operand);
    		else if (operand.type() == typeid(float))
    			return os << any_cast<float>(operand);
    		else if (operand.type() == typeid(double))
    			return os << any_cast<double>(operand);
    		else if (operand.type() == typeid(bool))
    			return os << any_cast<bool>(operand);
    		else if (operand.type() == typeid(char*))
    			return os << any_cast<char*>(operand);
    #ifdef _STRING_		// make sure that you won't get an error if not included
    		else if (operand.type() == typeid(std::string))
    			return os << any_cast<std::string>(operand);
    #endif
    		return os;	// if it wasn't one of the other types, it ignores it.
    	}
    Code:
    template <istream>
    	istream& operator >>(istream & is, any & operand)
    	//precondition:  input stream is open for reading
    	//postcondition: the next string from input stream is has been read
    	//               and stored in operand
    	{
    		char temp[82];	// not longer than screen length
    		char ch;
    		int i = 0;
        
    		while (i <= 80 && is.get(ch) && ch != '\n')
    			temp[i++] = ch;
    
    		temp[i] = '\0';
    
    		//cout << any_cast<char*>(operand) << endl;
    
    		if (isint(temp))			// if it is an integer, convert it to one
    			operand = atoi(temp);
    		else if (isfloat(temp))		// if it is a float or double, convert it to one
    			operand = atof(temp);
    		else						// otherwise, just store a string
    		{
    			char * newtemp = new char[strlen(temp)];
    			strcpy(newtemp,temp);
    			operand = newtemp;
    		}
    		//	operand = strdup(temp);	// allocate memory for this so that it isn't lost
    			//free(static_cast<any::holder<char *> *>(operand.content)->held);
       
    		return is;
    	}
    Code:
    template <any, typename UnknownType>
    	any operator <<(const any & lhs, const UnknownType & rhs)
    	{
    		any result;
    
    		if (lhs.type() == typeid(UnknownType))	// check that types are same.
    												// don't use float or double!
    			result = any_cast<UnknownType>(lhs) << rhs;
    
    		return result;   // returns a copy of result
    	}
    Code:
    	template <typename UnknownType>
    	UnknownType operator <<(const UnknownType & lhs, const any & rhs)
    	{
    		UnknownType result;
    
    		if (rhs.type() == typeid(UnknownType))	// check that types are same.
    												// don't use float or double!
    			result = lhs << any_cast<UnknownType>(rhs);
    
    		return result;   // returns a copy of result
    	}
    Code:
    	template <any, typename UnknownType>
    	any operator >>(const any & lhs, const UnknownType & rhs)
    	{
    		any result;
    
    		if (lhs.type() == typeid(UnknownType))	// check that types are same.
    												// don't use float or double!
    			result = any_cast<UnknownType>(lhs) >> rhs;
    
    		return result;   // returns a copy of result
    	}
    Code:
    	template <typename UnknownType>
    	UnknownType operator >>(const UnknownType & lhs, const any & rhs)
    	{
    		UnknownType result;
    
    		if (rhs.type() == typeid(UnknownType))	// check that types are same.
    												// don't use float or double!
    			result = lhs >> any_cast<UnknownType>(rhs);
    
    		return result;   // returns a copy of result
    	}
    Its having trouble figuring out which overloads to do.
    I believe this is because it tries to do the one with UnknownType as parameter 1 instead of the ostream/istream...

    What am I doing wrong?

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    here's the most recent version...

    Its in overloads.cpp, the commented out chunk in the middle.

    I'd be extremely greatful if anyone could help me!
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New stream flushing FAQ candidate?
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 07-01-2009, 05:57 PM
  2. Discard wrong data from stream
    By mesmer in forum C Programming
    Replies: 7
    Last Post: 11-16-2008, 01:30 AM
  3. Closing a stream
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2008, 04:15 PM
  4. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM
  5. Replies: 9
    Last Post: 07-01-2002, 07:50 AM

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