473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help converting this Write to operator<< overload

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
3 1820
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gianni Mariani | last post by:
Can anyone enligten me why I get the "ambiguous overload" error from the code below: friendop.cpp: In function `int main()': friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,...
14
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> ...
4
by: bluekite2000 | last post by:
Here A is an instantiation of class Matrix. This means whenever user writes Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2 //and fills it up w/ random value cout<<A; the...
25
by: Steve Richter | last post by:
is it possible to overload the << operator in c# similar to the way it is done in c++ ? MyTable table = new MyTable( ) ; MyTableRow row = new MyTableRow( ) ; row << new MyTableCell( "cell1 text...
7
by: glen | last post by:
Hi. I'm using GCC 4.1.1, which I mention since I don't know if this is a compiler issue, or me not understanding some subtlety in the standard. The code below compiles fine under vc++, but I'm...
1
by: ruchirp | last post by:
Hi, I've been trying to overload << for a function similar to boost lambda library. I want to make for_each(v.begin(), b.end(), cout << _1 << endl); to work, however, i'm getting a lot of problems...
5
by: krzysztof.konopko | last post by:
I cannot compile the code which defines a std::map type consisting of built in types and operator<< overload for std::map::value_type. See the code below - I attach a full example. Note: if I...
2
by: soy.hohe | last post by:
Hi all I have a class StreamLogger which implements operator << in this way: template <class TStreamLogger& operator<<(const T& t) { <print the stuff using fstream, etc.> return *this; }
3
by: Martin T. | last post by:
Hello. I tried to overload the operator<< for implicit printing of wchar_t string on a char stream. Normally using it on a ostream will succeed as std::operator<<<std::char_traits<char> will...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.