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: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 >>>>>>>>>>>>>>>>>>>>
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: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 >>>>>>>>>>>>>>>>>>>>