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

  1. #16
    Confused Magos's Avatar
    Join Date
    Sep 2001
    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...
    int Age = 21;
    String str("Nada!");
    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 09:37 AM.

  2. #17
    Registered User
    Join Date
    Sep 2002
    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 -

     #include <string>
    #include <iostream>
    using namespace std;
    struct my_string
    	string s;
    	enum clear { not_clear = 0, to_clear = 1 };
    	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)
    		return os<<m.s;
    	void SetString(char* c)
    		s = c;
    //the iostream manipulator
    std::ostream& Clear(std::ostream& os)
    	return os;
    int main()
        my_string m("Joe");
    	cout << m << Clear <<  m;
    	cout << m;
        return 0;

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, 04:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM