J
Jim Langston
I have a CSkill class which is rather complex as it is recursive. That is:
class CSkill
{
public:
CSkill( std::string Name, float Value ): Name_( Name ), Value_( Value )
{};
void Update( const std::string& Name, const float Value );
float Value( const std::string& Name ) const;
float Sum( const std::string& Name ) const;
void Write( std:stream& os, const std::string& Prefix ) const;
std::string CSkill:lainName( const std::string& Name ) const;
friend std::istream& operator>>( std::istream& is, CSkill& Skill);
private:
float Sum() const;
std::string Name_;
float Value_;
std::map< std::string, CSkill > Skills_;
};
Well, to output this class to an ostream, I use this function:
void CSkill::Write( std:stream& os, const std::string& Prefix ) const
{
if ( Name_.length() > 0 && Value_ > 0 )
os << Prefix << Name_ << " " << Value_ << std::endl;
for ( std::map< std::string, CSkill >::const_iterator it =
Skills_.begin(); it != Skills_.end(); ++it )
{
(*it).second.Write( os, ( Name_.length() > 0 ? Prefix + Name_ + "|"
: "" ) );
}
}
but I would prefer to use
std:stream& operator<<( /* what goes here? */ )
The problem I see is that I need to pass that extra parameter, a
std::string, which is used in the recursion. Is something like this
allowed?
outfile << Skills( "" ) << SomethingElse
I don't think so because a "normal" declaration of the operator<< would be
something like:
std:stream& operator<<( std:stream& os, CCharacter& CChar)
and so I would have to make it something like:
std:stream& operator<<( std:stream& os, const CSkill& Skill, const
std::string& Prefix )
But the compiler complains (no suprise):
error C2804: binary 'operator <<' has too many parameters
Any suggestions or do I just have to stick with the way I'm doing it?
class CSkill
{
public:
CSkill( std::string Name, float Value ): Name_( Name ), Value_( Value )
{};
void Update( const std::string& Name, const float Value );
float Value( const std::string& Name ) const;
float Sum( const std::string& Name ) const;
void Write( std:stream& os, const std::string& Prefix ) const;
std::string CSkill:lainName( const std::string& Name ) const;
friend std::istream& operator>>( std::istream& is, CSkill& Skill);
private:
float Sum() const;
std::string Name_;
float Value_;
std::map< std::string, CSkill > Skills_;
};
Well, to output this class to an ostream, I use this function:
void CSkill::Write( std:stream& os, const std::string& Prefix ) const
{
if ( Name_.length() > 0 && Value_ > 0 )
os << Prefix << Name_ << " " << Value_ << std::endl;
for ( std::map< std::string, CSkill >::const_iterator it =
Skills_.begin(); it != Skills_.end(); ++it )
{
(*it).second.Write( os, ( Name_.length() > 0 ? Prefix + Name_ + "|"
: "" ) );
}
}
but I would prefer to use
std:stream& operator<<( /* what goes here? */ )
The problem I see is that I need to pass that extra parameter, a
std::string, which is used in the recursion. Is something like this
allowed?
outfile << Skills( "" ) << SomethingElse
I don't think so because a "normal" declaration of the operator<< would be
something like:
std:stream& operator<<( std:stream& os, CCharacter& CChar)
and so I would have to make it something like:
std:stream& operator<<( std:stream& os, const CSkill& Skill, const
std::string& Prefix )
But the compiler complains (no suprise):
error C2804: binary 'operator <<' has too many parameters
Any suggestions or do I just have to stick with the way I'm doing it?