Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overloading Operators...

Status
Not open for further replies.

Phred

Programmer
Apr 6, 1999
20
0
0
US
I'm wondering in general about how to overload an operator, specifically when using the "cout" command. Sample source code would be a great help.
 
Every book about C++ that I own has examples on overloading the << and >> operators. Try looking at your book again.
 
Every book about C++ that I own has examples on overloading the << and >> operators. Try looking at your book again.
 
I'm searching how to overload the operator<< in a subclass of ofstream (not ostream!)
I can't understand why it work with ostream but not with ofstream (and book example are for ostream :()
 
Look one sample:
#include<iostream>
using namespace std;
class x
{
public:
int xx;
};
ostream& operator<<(ostream& c,x& a)
{
return c<<a.xx;
}
int main()
{
x a;
a.xx=0;
cout<<a<<endl;
return 0;
} John Fill


ivfmd@mail.md
 
Doing it for fstream works fine. I have always put the overloading into the .h files or into the structs i create and just inline them. Here is an example.

friend ofstream& operator << (ofstream& o,
<user defined type> my_type)
{
...// output my_type how you want (output to &quot;o&quot;)
return o;
}

the same goes for all other streams.

Matt
 
Akay.. so I will try again with another explanation (as nothing work :()

I want to creat a fonction/class which accept to use the operator<< with a string like this:

TOutput l_output;
_output.open(&quot;test.fichier&quot;, ofstream::eek:ut | ofstream::app);
_output<< &quot;Output\n&quot;;
_output.close();

and in the &quot;test.fichier&quot; file I want to have:
SysLog Output

So the problem is:
How to make an operator like this one
friend ofstream& operator << (ofstream& o,
<user defined type> my_type)
{
...// output my_type how you want (output to &quot;o&quot;)
return o;
}

returning the string o = &quot;Syslog &quot;+mystring;

*Sob* One day and I still can't find how to do :(

 
If I understand this correctly, i think all you need to do is when you declare your ofstream, open it how you would like (i.e. ios::app | ios::eek:ut)

example

class foo{
public:
...
friend ofstream& operator << (ofstream& O,
foo my_foo)
{
O<<my_foo.value1<<endl<<my_foo.value2;
return O;
}

private:
int value1,value2;
};

int main()
{
ofstream output(&quot;output.txt&quot;,ios::app|ios::eek:ut);

foo my_foo(1,2);// sets value1 to 1 and value2 to 2
...
output<<my_foo;

return 0;
}

// expected output is:
// 1
// 2


Matt
 
Yes but.. it does not allow me to transform the
(ofstream)l_output << &quot;This string&quot;;
to append a file with
SysLog This string

:(

Thank you
 
SOLVED:
void TFichier::eek:perator<<(char* p_string)
{
ofstream l_out(&quot;File.txt&quot;, ios::eek:ut | ios::app);
_donnee = p_chaine; // _donnee is a class value
l_out << &quot;SysLog &quot; << _donnee;
l_sortie.close();
}

Sorry.. it was simple but I miss the point :( :(

lol
 
Hem.. now I have the recurrent problem..

It write &quot;Syslog &quot; each time I use the operator <<..


Like in l_log << &quot;Test&quot; << 6548;

Result: Syslog TestSyslog 6548..

*Sob :)*
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top