By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,351 Members | 1,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,351 IT Pros & Developers. It's quick & easy.

Help converting this Write to operator<< overload

P: n/a
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::ostream& os, const std::string& Prefix ) const;

std::string CSkill::PlainName( 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::ostream& 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::ostream& 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::ostream& operator<<( std::ostream& os, CCharacter& CChar)

and so I would have to make it something like:
std::ostream& operator<<( std::ostream& 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?
May 12 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Jim Langston <ta*******@rocketmail.com> wrote:
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::ostream& os, const std::string& Prefix ) const;

std::string CSkill::PlainName( 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::ostream& 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::ostream& 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


Could you do something like:

std::ostream& operator<<(std::ostream& o, const CSkill& c)
{
c.Write(o, "");
return o;
}

?

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 12 '06 #2

P: n/a
Jim Langston wrote:
I have a CSkill class which is rather complex as it is recursive. That is:

[snip (a rather lengthy class declaration)]

Well, to output this class to an ostream, I use this function:

void CSkill::Write( std::ostream& os, const std::string& Prefix ) const
{
[snip (we don't care)]
}

but I would prefer to use
std::ostream& 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


If you like so. One way would be add operator () to CSkill like this:

class CSkill
{
private:
std::string m_pAdditionalParameter;
public:
CSkill& operator () (const std::string& p_pAdditionalParameter)
{
m_pAdditionalParameter = p_pAdditionalParameter;
return *this;
}

// Now the rest of your class.
};

The operator<< will now look like this:

std::ostream& operator<< (std::ostream& os, CSkill& p_Skill)
{
p_Skill.Write (os, m_pAdditionalParameter);
return os;
}
Regards,
Stuart
May 12 '06 #3

P: n/a

"Marcus Kwok" <ri******@gehennom.invalid> wrote in message
news:e4**********@news-int.gatech.edu...
Jim Langston <ta*******@rocketmail.com> wrote:
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::ostream& os, const std::string& Prefix ) const;

std::string CSkill::PlainName( 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::ostream& 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::ostream& 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


Could you do something like:

std::ostream& operator<<(std::ostream& o, const CSkill& c)
{
c.Write(o, "");
return o;
}

?


I actually thought about that about 2 minutes before checking for responses.
That's a good idea and I think that's the way I'll go. The parm going into
the top level is an empty string "" so I don't have to change it.

Good thinking.
May 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.