Thread: Issue overloading stream Operator/Manipulator

  1. #1
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256

    Issue overloading stream Operator/Manipulator

    I've got an interesting issue trying to make an ofstream container class. For the moment, I'm trying to just pass through the << operator to the underlying ofstream object, as such

    Code:
    class CfgFile
     {
      .....
      template <typename _T>
        CfgFile& operator<< (_T arg);
      .....
     };
    But this causes problems when using...

    Code:
    OutputFile << endl;
    ...per the following error.

    Code:
    error: no match for ‘operator<<’ in ‘OutputFile << std::endl’
    However, overloading the operator manually, as follows, without templating, eliminates the error.

    Code:
    CfgFile& operator<< (bool& arg);
    CfgFile& operator<< (short& arg);
    CfgFile& operator<< (unsigned short& arg);
    CfgFile& operator<< (int& arg);
    CfgFile& operator<< (unsigned int& arg);
    CfgFile& operator<< (long& arg);
    CfgFile& operator<< (unsigned long& arg);
    CfgFile& operator<< (float& arg);
    CfgFile& operator<< (double& arg);
    CfgFile& operator<< (long double& arg);
    CfgFile& operator<< (const void* arg);
    CfgFile& operator<< (char arg);
    CfgFile& operator<< (signed char arg);
    CfgFile& operator<< (unsigned char arg);
    CfgFile& operator<< (const char* arg);
    CfgFile& operator<< (const signed char* arg);
    CfgFile& operator<< (const unsigned char* arg);
    CfgFile& operator<< (streambuf* arg);
    CfgFile& operator<< (ostream& ( *arg )(ostream&));
    CfgFile& operator<< (ios& ( *arg )(ios&));
    CfgFile& operator<< (ios_base& ( *arg )(ios_base&));
    Can anyone who knows templating a little better than I do shed some light?

    Thanks in advance.

    ~ Jake
    Code:
    void function(void)
     {
      function();
     }

  2. #2
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    Custom operator<<'s should be nonmember friends. But I think a bigger problem is why do you need an ofstream container class? What is CfgFile supposed to do that ofstream can not do? There is probably a better solution to the problem than what you are doing now.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    FWIW, your _T identifier runs afoul of the rules for such things:
    7.1.3 Reserved identifiers

    Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.

    • All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    when calling ostream:perator << endl, it resolves to this:

    Code:
    	_Myt& __CLR_OR_THIS_CALL operator<<(_Myt& (__cdecl *_Pfn)(_Myt&))
    		{	// call basic_ostream manipulator
    		_DEBUG_POINTER(_Pfn);
    		return ((*_Pfn)(*this));
    		}
    which, in this case, is equivalent to your overload:

    Code:
    CfgFile& operator<< (ostream& ( *arg )(ostream&));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operator overloading compilation issue in GCC
    By redneon in forum C++ Programming
    Replies: 5
    Last Post: 03-13-2011, 07:53 PM
  2. Extracting from stream issue
    By keira in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2008, 08:56 PM
  3. Overloading Issue
    By CornedBee in forum C++ Programming
    Replies: 7
    Last Post: 09-23-2007, 06:03 AM
  4. Simple operator overloading issue
    By Desolation in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2007, 08:56 PM
  5. Overloading << Issue
    By dld333 in forum C++ Programming
    Replies: 1
    Last Post: 10-27-2005, 02:18 AM