custom manipulators gone wrong

S

sakis.panou

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::eek:perator<<(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 >>>>>>>>>>>>>>>>>>>>
 
T

Thomas J. Gritzan

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::eek:perator<<(simple&
(*)(simple&))
main.cpp:88: note: simple& operator<<(simple&,
simplemanip&) [...]

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

This operator takes a non-const reference to a simplemanip.
simple& _reset( simple& s, unsigned long offset )
{
return( s.reset( offset ) );
}

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

This function returns a temporary simplemanip object.
int main( int argc, char* argv[] )
{
simple s;

s << setflag1;

s << reset( 10 );

Here you try to bind a temporary simplemanip object to a non-const
reference, thats forbidden by the language standard. Change operator<< to
take a const reference.
 
S

sakis.panou

Hi Thomas,

Thanks very much for this one. Much appreciated. You were right on the
money there.

Best Regards,
Sakis

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::eek:perator<<(simple&
(*)(simple&))
main.cpp:88: note: simple& operator<<(simple&,
simplemanip&)
[...]

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

This operator takes a non-const reference to a simplemanip.
simple& _reset( simple& s, unsigned long offset )
{
return( s.reset( offset ) );
}
inline simplemanip reset( unsigned long ulOffset )
{
return( simplemanip( _reset, ulOffset ) );
}

This function returns a temporary simplemanip object.
int main( int argc, char* argv[] )
{
simple s;
s << setflag1;
s << reset( 10 );

Here you try to bind a temporary simplemanip object to a non-const
reference, thats forbidden by the language standard. Change operator<< to
take a const reference.
return( 0 );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top