Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > custom manipula...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 3 Topic 44225 of 48022
Post > Topic >>

custom manipulators gone wrong

by sakis.panou@[EMAIL PROTECTED] Mar 30, 2008 at 11:51 AM

Hi all,

I have been trying to create a custom manipulator that takes one
parameter in a very simple class.
I have followed Stroustrup's example in chapter 21 of his 3rd Edition
"The C++ Programming Language", in so far as I understand it, it
should work, however, I do think something very silly is going on here
and for the life of me I just can't see it. I would appreciate any
help you kind folk are willing to impart with.

I am trying to compile this example with gcc 4.1.2, however, the
output I am getting is:

main.cpp: In function 'int main(int, char**)':
main.cpp:109: error: no match for 'operator<<' in 's << reset(10ul)'
main.cpp:35: note: candidates are: simple& simple::operator<<(simple&
(*)(simple&))
main.cpp:88: note:                 simple& operator<<(simple&,
simplemanip&)


<<<<<<<<<<<<<<< Start of code snippet >>>>>>>>>>>>>>>>>>>>

#include <iostream>

class simple
{
    public:

        explicit simple() :
            m_ulOffset( 0 ),
            m_Flags( enFlag1 )
        {
        };

        virtual ~simple()
        {
        };

        simple& reset( unsigned long ulOffset )
        {
            m_ulOffset = ulOffset;
            return( *this );
        };

        simple& setflag1( void )
        {
            m_Flags = enFlag1;
            return( *this );
        };

        simple& setflag2( void )
        {
            m_Flags = enFlag2;
            return( *this );
        };

        simple& operator<<( simple& (*f)( simple& ) )
        {
            return( f( *this ) );
        };

    private:

        enum AtFlags
        {
            enFlag1,
            enFlag2
        };

        unsigned long m_ulOffset;
        AtFlags m_Flags;
};

//
// Non parameterised manipulators
//
simple& setflag1( simple& s )
{
    return( s.setflag1() );
}

simple& setflag2( simple& s )
{
    return( s.setflag2() );
}

//
// Function pointer object
//
class simplemanip
{
    public:
        simplemanip(
            simple& ( *pfn_prm )( simple& s_prm, unsigned long
ul_prm ),
            unsigned long offset_prm ) :
            pfn( pfn_prm ),
                 offset( offset_prm )
        {
        };

        simple& ( *pfn )( simple& s, unsigned long ul );
        unsigned long offset;
};

simple& operator<<( simple& s, simplemanip& m )
{
    return( m.pfn( s, m.offset ) );
}

simple& _reset( simple& s, unsigned long offset )
{
    return( s.reset( offset ) );
}

inline simplemanip reset( unsigned long ulOffset )
{
    return( simplemanip( _reset, ulOffset ) );
}

int main( int argc, char* argv[] )
{
    simple s;

    s << setflag1;

    s << reset( 10 );

    return( 0 );
}

<<<<<<<<<<<<<<< End of code snippet >>>>>>>>>>>>>>>>>>>>
 




 3 Posts in Topic:
custom manipulators gone wrong
sakis.panou@[EMAIL PROTEC  2008-03-30 11:51:44 
Re: custom manipulators gone wrong
"Thomas J. Gritzan&q  2008-03-30 21:37:16 
Re: custom manipulators gone wrong
sakis.panou@[EMAIL PROTEC  2008-03-30 12:47:42 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 11:44:49 CDT 2008.