"Operator must be a member function..." (Error)

This is a discussion on "Operator must be a member function..." (Error) within the C++ Programming forums, part of the General Programming Boards category; You have a point there, Sang-Drax (what does Sang-Drax mean anyway? ). I think I will stick with the << ...

  1. #16
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You have a point there, Sang-Drax (what does Sang-Drax mean anyway? ).

    I think I will stick with the << operators, which will make my String object function like cout and other stream objects.

    Still sux that you need to clear it though...
    Code:
    int Age = 21;
    String str("Nada!");
    
    str.Clear();
    str << "I am " << Age << " years old!";
    BTW: How do you use methods to be called in this way?
    (like cout << endl; )

    I tried str << Clear, but that didn't work.
    Last edited by Magos; 10-28-2002 at 08:37 AM.

  2. #17
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    You'll have to create a manipulator. You can either create a user defined manipulator by using the implementation defined smanip structure (in iomanip), or create a flag that'll enable your operator << to establish whether your string needs clearing. Something like -

    Code:
     #include <string>
    #include <iostream>
    
    using namespace std;
    
    
    struct my_string
    {
    	string s;
    	enum clear { not_clear = 0, to_clear = 1 };
    
    	//constructor
    	my_string(char* p){s=p;}
    	
    	//Obtain unique index for flag
    	static long& my_clear_flag(std::ios_base& s) 
    	{
    		//Ensure that calling clear is for the correct
    		//instance using static member xalloc
      		static int my_index = std::ios_base::xalloc();
      		
    		return s.iword(my_index);
    	}
    
    	static clear get_clear(std::ios_base& s)
    	{ 
    		return (clear) my_clear_flag(s);
    	}
    
    
    	static void set_clear(std::ios_base& s)
    	{ 
    		my_clear_flag(s) = ~get_clear(s);
    	}
    	
    	friend ostream& operator<<(ostream& os, my_string& m)
    	{
    		if(get_clear(os))
    		{
    			m.s="";
    			set_clear(os);
    		}
    		return os<<m.s;
    	}
    	
    	void SetString(char* c)
    	{
    		s = c;
    	}
        
    };
    
    
    //the iostream manipulator
    std::ostream& Clear(std::ostream& os)
    {
    	my_string::set_clear(os);
    	return os;
    }
    
    
    int main()
    {
        my_string m("Joe");
    
    	cout << m << Clear <<  m;
    
    	m.SetString("Sixpack");
    
    	cout << m;
    	
        return 0;
    }
    Joe

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

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