Can't get rid of trash while overloading operator << in C++ - c++

class point //declaration of class
{
private:
int x, y;
friend std::ostream &operator << (std::ostream &input, point &p);
public:
//constructors and some other methods
};
//definition of overloading <<
std::ostream &operator << (std::ostream &input, point &p)
{
input << std::cout << "x = " << p.x << " y = " << p.y << " ";
return input;
}
And it works but when I use it
std::cout << object;
it shows some trash before my text:
062ACC3E8x = 1 y = 22
So 062ACC3E8X is something that always appears. It is defferent if I restart Visual Studio which I'm working on, so I suppose It's some memory adress. How to get rid of it? Is it something missing or wrong in my code?

You output some adress, since std::ostream has implicit void* conversion operator.
1) Returns a null pointer if fail() returns true, otherwise returns a
non-null pointer. This pointer is implicitly convertible to bool and
may be used in boolean contexts.
Should be just
input << "x = " << p.x << " y = " << p.y << " ";

You are passing std::cout into your output stream. Change your code to:
//definition of overloading <<
std::ostream &operator << (std::ostream &input, point &p)
{
input << "x = " << p.x << " y = " << p.y << " ";
return input;
}

Related

C++ access object behind iterator in loop

I have created a list of objects of a class.
The class has an overloaded ostream << operator to output customer data in a structured way.
What I am trying to do is loop over the list of objects and call cout on the object in the iteration.
Code for the loop is as follows:
for (list<Kunde>::iterator it = this->kun_list.begin(); it != this->kun_list.end(); ++it) {
cout << it << endl;
}
With Kunde being the class with the overloaded << operator and kun_list being the list of objects of type Kunde.
friendly overload within the Kunde class:
friend ostream& operator<< (ostream& os, Kunde& kd) {
os << "__Kundendaten__" << endl;
os << "Name: " << kd.vorname << " " << kd.name << endl;
os << "Geburtsjahr: "<< kd.geburtsjahr << endl;
os << "Adresse: " << kd.strasse << " " << kd.hausnummer << endl << kd.plz << " " << kd.ort << endl;
os << "Telefon: " << kd.telefonnummer << endl;
string fschein = "Nein.";
if (kd.klasse_a_vorhanden) {fschein = "Ja.";}
os << "Führerschein Kl. A vorhanden: " << fschein << endl;
return os;
};
The above loop does not work because I am using the list iterator instead of an object of class Kunde. I can access members of Kunde via it→member but how do I use that iterator as reference to the whole object?
Thanks!
Use a const reference loop over the container:
for (const auto & kunde : kun_list) {
cout << kunde << endl;
}
Obviously you also have to fix <<:
friend ostream& operator<< (ostream& os, const Kunde& kd) {...}

C++ operator overloading stream requires cast

I'm overloading some operation on a class and notice that on the ostream overloading one of the values is not as expected. Here is the example:
#include <iostream>
using namespace std;
class Simple{
int x;
public:
Simple() : x(0){}
Simple(int i) : x(i){}
const Simple& operator+(const Simple& s){
return Simple(x + s.x);
}
friend ostream& operator<<(ostream& os, const Simple& s);
};
ostream& operator<<(ostream& os, const Simple& s){
os << s.x;
return os;
}
int main(void){
Simple s1(2);
Simple s2(3);
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "s1 + s2 = ";
Simple s3 = s1 + s2;
cout << s1 << " + " << s2 << " = " << s1 + s2 << endl;
cout << s1 << " + " << s2 << " = " << static_cast<Simple>(s1 + s2) << endl;
cout << "s3 = " << s3 << endl;
return 0;
}
It produces the following output:
s1 = 2
s2 = 3
s1 + s2 = 2 + 3 = -872483744
2 + 3 = 5
s3 = 5
Is this an issue with how I am overloading, or will the cast always be required to get my expected output?
Your operator+ returns a reference to a local variable, and a temporary at that.
It's a dangling reference.
operator+ should return by value.

C++ - Overloaded Operator in Derived Class not working

Okay, so the overloaded operator for the derived class is not working. It is only using the overloaded operator in the base class. Any ideas why?
Base class operator in class definition header file:
friend ostream & operator << (ostream & out, const PocketMonster & p);
Base class operator:
ostream & operator << (ostream & out, const PocketMonster & p)
{
out << endl << "(Monster Types: Type 1 = Fire, Type 2 = Water, Type 3 = Grass)" << endl
<< "PocketMonster Information: " << endl << "Name: " << p.name << endl
<< "Status (0=Dead, 1=Alive): " << p.status << endl << "Level: " << p.level << endl
<< "Strength: " << p.strength << endl
<< endl << "(Weapon Types: 1 = Fire, 2 = Water, 3 = Grass, 4 = Normal)" << endl
<< p.name << "'s Weapon Information: " << endl << "Weapon type: " << p.get_weaptype() << endl
<< "Weapon durability: " << p.get_weapdura() << endl << "Weapon level required: " << p.get_weaplvl() << endl << endl;
return out;
}
Derived class overloaded operator in class definition header file:
friend ostream & operator << (ostream & out, const FireMonster & p);
Derived class overloaded operator:
ostream & operator << (ostream & out, const FireMonster & p)
{
return out << static_cast<const PocketMonster&>(p) << endl << "FireMonster Attributes:" << endl << "Temperature: " << p.temperature << endl;
}
And here's a function where it tries to output the information
void displayLosers(vector<PocketMonster *> p)
{
for (int i=0; i<p.size(); i++)
{
if (p[i]->get_status() == false)
{
cout << p[i]->get_name() << " is a loser." << endl;
cout << *(p[i]);
}
}
}
Thanks for help in advance!
Add virtual function Output to the base class (at public or protected section):
virtual ostream & Output (ostream & out) const;
// ...
ostream & PocketMonster::Output (ostream & out) const
{
out << endl << "(Monster Types: Type 1 = Fire, Type 2 = Water, Type 3 = Grass)" << endl
<< "PocketMonster Information: " << endl << "Name: " << p.name << endl
<< "Status (0=Dead, 1=Alive): " << p.status << endl << "Level: " << p.level << endl
<< "Strength: " << p.strength << endl
<< endl << "(Weapon Types: 1 = Fire, 2 = Water, 3 = Grass, 4 = Normal)" << endl
<< p.name << "'s Weapon Information: " << endl << "Weapon type: " << p.get_weaptype() << endl
<< "Weapon durability: " << p.get_weapdura() << endl << "Weapon level required: " << p.get_weaplvl() << endl << endl;
return out;
}
and override it in derived class:
virtual ostream & Output (ostream & out) const;
// ...
ostream & FireMonster::Output (ostream & out) const
{
return out << PocketMonster::Output(out) << endl << "FireMonster Attributes:" << endl << "Temperature: " << p.temperature << endl;
}
Then rewrite operator<< for the base class in the following way:
ostream & operator << (ostream & out, const PocketMonster & p)
{
return p.Output(out);
}
and remove operator<< for derived class.

Errors with operator overloading

i have two different operator overloading. For some reason it is giving error.
If i remove one of it, than it does not show any error. May i know why ?
Can i combine both ?
This is used for printing on screen.
ostream& operator<<(ostream &out, const Point &p) {
return out << "[" << setw(4) << p.getX() << setw(1) << "," << setw(4) << p.getY() << "] " << setprecision(3) << p.getScalarValue() << endl;
}
This is used for printing on a text file.
ofstream& operator<<(ofstream &out, const Point2D &p){
return out << "[" << setw(4) << p.getX() << setw(1) << "," << setw(4) << p.getY() << "] " << setprecision(3) << p.getScalarValue() << endl;
}
Error:
Point.cpp:91:147: error: invalid initialization of reference of type ‘std::ofstream& {aka std::basic_ofstream&}’ from expression of type ‘std::basic_ostream::__ostream_type {aka std::basic_ostream}’
You do not need the second version. You can use the first:
Point p;
std::ofstream pointsFile("points.txt");
pointsFile << p << "\n";
First, The std::ostream& operator<< works for writing to files as well as writing to the standard output or stderrt
Second, assuming Poind2D inherits from Point, passing a Point2D to a function or operator that takes a Point reference will work too.

using setw with user-defined ostream operators

How do I make setw or something similar (boost format?) work with my user-defined ostream operators? setw only applies to the next element pushed to the stream.
For example:
cout << " approx: " << setw(10) << myX;
where myX is of type X, and I have my own
ostream& operator<<(ostream& os, const X &g) {
return os << "(" << g.a() << ", " << g.b() << ")";
}
Just make sure that all your output is sent to the stream as part of the same call to operator<<. A straightforward way to achieve this is to use an auxiliary ostringstream object:
#include <sstream>
ostream& operator<<(ostream& os, const X & g) {
ostringstream oss;
oss << "(" << g.a() << ", " << g.b() << ")";
return os << oss.str();
}
maybe like so using the width function:
ostream& operator<<(ostream& os, const X &g) {
int w = os.width();
return os << "(" << setw(w) << g.a() << ", " << setw(w) << g.b() << ")";
}