473,796 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::PlainNa me( 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_iterat or it =
Skills_.begin() ; it != Skills_.end(); ++it )
{
(*it).second.Wr ite( 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 1843
Jim Langston <ta*******@rock etmail.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::PlainNa me( 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_iterat or it =
Skills_.begin() ; it != Skills_.end(); ++it )
{
(*it).second.Wr ite( 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_pAdditionalPa rameter;
public:
CSkill& operator () (const std::string& p_pAdditionalPa rameter)
{
m_pAdditionalPa rameter = p_pAdditionalPa rameter;
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_pAdditionalPa rameter);
return os;
}
Regards,
Stuart
May 12 '06 #3

"Marcus Kwok" <ri******@gehen nom.invalid> wrote in message
news:e4******** **@news-int.gatech.edu. ..
Jim Langston <ta*******@rock etmail.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::PlainNa me( 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_iterat or it =
Skills_.begin() ; it != Skills_.end(); ++it )
{
(*it).second.Wr ite( 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
5015
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, std::char_traits<char> >& << Thing&' operator friendop.cpp:27: candidates are: std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>&, const Thing&) friendop.cpp:14: std::basic_ostream<_CharT, _Traits>&...
14
2339
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> using namespace std;
4
2065
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 following will be printed A 3 2
25
2856
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 contents" ) ; row << new MyTableCell( "cell2 text contents" ) ; table << row ; thanks,
7
14927
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 having trouble using gcc. It's just a templated container output. /** outputs elements of a vector in the form '{el1,el2...elEnd}'
1
1408
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 in making this happen.. The following is my current code > #include <string> #include <iostream> using namespace std;
5
3659
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 define map type with my new type (structure) everything is OK. All compileres I've cheked report an error so I think it is a problem with my code. #include <algorithm> #include <cstddef>
2
2845
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
4278
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 be called. However, I am using the boost::format library that internally makes use of a string stream to store the formatted string. There:
0
9679
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10223
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.