I am trying to overload the << operator by using the code
inline ostream& operator<< (ostream& out, Node& n){n.print(out); return out;}
and the print function I call is just
void Node::print(ostream& out){
out<< freq << " " << input<<" " << Left<< " " << Right<< endl;
}
Left and right are both pointers that print out in hex when I just call the print function. But when I use the << operator it just prints everything in hex i.e 0x600084d40. I don't want it printing out in Hex I want the values of freq and input when and the two hex pointers when I print it.
When I try to print it out I am printing a Node* i dont know if that has anything to do with it.
Thank you for any help.
When I try to print it out I am printing a Node* i dont know if that has anything to do with it.
It definitely does.
Node* n = ...;
std::cout << n;
invokes overload that just prints a pointer. You need to use:
Node* n = ...;
std::cout << *n;
If you want
std::cout << n;
to work similar to
std::cout << *n;
you'll have to provide an overload.
inline ostream& operator<< (ostream& out, Node* n)
{
return (out << *n);
}
Suggested Improvements
operator<< functions should use const&, not a non-const reference.
inline ostream& operator<<(ostream& out, Node const& n);
That would necessitate changing print to a const-member function.
I would also recommend changing the return type of print to std::ostream&.
std::ostream& print(std::ostream& out) const;
Now, the implementations would look like:
std::ostream& Node::print(std::ostream& out)
{
return (out<< freq << " " << input<<" " << Left<< " " << Right<< std::endl);
}
inline std::ostream& operator<<(std::ostream& out, Node const& n)
{
return n.print(out);
}
inline std::ostream& operator<<(std::ostream& out, Node const* n)
{
return (out << *n);
}
Why use the print function? Instead you could just use overloading with friend functions. Previously when implementing this I needed to pass the object by const reference like this const Node &nameand also return an ostream&
Assuming that freq and input are members of the class, your code should look like this in your .cpp:
ostream& operator<<(ostream& out, const Node& n)
{
return out<<n.freq<< " " <<n.input<<" "<<n.Left<<" "<< n.Right;
}
and this in the .h:
friend ostream& operator<<(ostream& out, const Node& n);
If this needs to take pointers you can simply modify it to be:
ostream& operator<<(ostream& out, const Node* n)
{
return out<<n->freq<< " " <<n->input<<" "<<n->Left<<" "<< n->Right;
}
and:
friend ostream& operator<<(ostream& out, const Node* n);
I hope this helps!
Related
I have created a constructor
Location(double xIn,double yIn,string placeIn,int timeIn)
: x(xIn),y(yIn) ...so on {
Say I want to print Location home(x,y,place,time); that's in the main().
How would I do so? I've been searching around and was told to use operator<<. How would I implement this?
UPDATE: After creating some get methods and I tried doing,can't exactly compile it because of the problem
ostream &operator<<(ostream & o, const Location & rhs){
o << rhs.getX() << "," << rhs.getY() << "," << rhs.getPlace() << "," << rhs.getTime();
return o; }
Here is the stencil for overloading operator<<:
class Any
{
public:
friend std::ostream& operator<<(std::ostream& output, const Any& a);
private:
int member;
};
std::ostream&
operator<<(std::ostream& output, const Any& a)
{
output << a.member;
return output;
}
This one possible stencil, there are other possibilities. Search the internet for "c++ stream insertion operator overload example" for other implementations.
I have some problems in understanding the << operator.
If I have:
#include <iostream>
using namespace std;
//...
int t = 5;
cout << "test is: " << t << endl;
Now the function operator<< is called.
ostream& operator<<(ostream& out, string* s)
{
return out << s << endl;
}
ostream& operator<<(ostream& out, int* value)
{
return out << value << endl;
}
the string-pointer points to the address with value test is: but to what does the element out refer (to cout?)? and is the function body of ostream& correct in that way?
Thank you so much for any explanation.
First, let's fix your code: the operators should be taking const references or values instead of pointers:
ostream& operator<<(ostream& out, const string& s) // const reference
ostream& operator<<(ostream& out, int i) // value
Now to your question: you are correct, the out parameter receives the reference to the cout, or whatever is the ostream& returned from the expression on the left side of <<. The expression on the left of << is not necessarily cout, though - other common cases are results of other << operators* for chaining, and stream manipulators. In all cases these expressions return a reference to ostream so that the "chain" could continue.
* The reason the operator<< return an ostream& is so that you could chain the output. In overwhelming number of cases you wold return the same ostream& that you receive as the first parameter, although there is no limitation on the part of the standard C++ library requiring you to do that.
int t = 5;
cout << "test is: " << t << endl;
First call would be to:-
ostream& operator<<(ostream& out, const char* str)
out = cout
str = "test is: "
Second call would be applied on object returned by first call which is ostream.
ostream& operator<<(ostream& out, int x)
out = cout
x = t
This is not true. It's int not int*, and char const* not string*.
out, of course, refers to std::cout in this example. What else would it be?
And no those bodies are not correct in the slightest — they attempt to reinvoke themselves infinitely, and do nothing else.
I'm a student learning c++. today, I was making a operator overload function to use it in 'cout'. following is a class that contains name, coordinates, etc.
class Custom {
public:
string name;
int x;
int y;
Custom(string _name, int x, int y):name(_name){
this->x = x;
this->y = y;
}
int getDis() const {
return static_cast<int>(sqrt(x*x+y*y));
}
friend ostream& operator << (ostream& os, const Custom& other);
};
ostream& operator << (ostream& os, const Custom& other){
cout << this->name << " : " << getDis() << endl;; // error
return os;
}
However, this code isn't working because of 'THIS' keyword that I was expecting it points to the object. I want to show the object's name and distance value. How can I solve it? I think it is similar with Java's toString method so that it will be able to get THIS.
Thanks in advance for your answer and sorry for poor english. If you don't understand my question don't hesitate to make a comment.
this is available only in member functions, but your operator<< is not a class member (declaring it as friend does not make it a member). It is a global function, as it should be. In a global function, just use the arguments you are passing in:
ostream& operator << (ostream& os, const Custom& other)
{
os << other.name << " : " << other.getDis() << endl;
return os;
}
Also note os replaced cout in the code above. Using cout was an error - the output operator should output to the provided stream, not to cout always.
I want to overload the << operator for my class Complex.
The prototype is the following:
void Complex::operator << (ostream &out)
{
out << "The number is: (" << re <<", " << im <<")."<<endl;
}
It works, but I have to call it like this: object << cout for the standart output.
What can I do to make it work backwards, like cout << object?
I know that the 'this' pointer is by default the first parameter sent to the method so that's why the binary operator can work only obj << ostream. I overloaded it as a global function and there were no problems.
Is there a way to overload the << operator as a method and to call it ostream << obj?
I would just use the usual C++ pattern of free function. You can make it friend to your Complex class if you want to make Complex class's private data members visible to it, but usually a complex number class would expose public getters for real part and imaginary coefficient.
class Complex
{
....
friend std::ostream& operator<<(std::ostream &out, const Complex& c);
private:
double re;
double im;
};
inline std::ostream& operator<<(std::ostream &out, const Complex& c)
{
out << "The number is: (" << c.re << ", " << c.im << ").\n";
return out;
}
You could write a free stand operator<< function, try:
std::ostream& operator<< (std::ostream &out, const Complex& cm)
{
out << "The number is: (" << cm.re <<", " << cm.im <<")." << std::endl;
return out;
}
You can define a global function:
void operator << (ostream& out, const Complex& complex)
{
complex.operator<<(out);
}
I have a polynomial implementation in a linked list and want to do std::ostream overloading operation but it gives my an error that no match for ‘operator<<’ in ‘std::cout << p5’
That's my implementation but when I test it through cout << p5 I get the aforementioned error.
UPDATE:
header file:
struct term{
double coef;
unsigned deg;
struct term * next;
};
class Polynomial {
public:
constructors etc
overloading functions
friend ostream& operator << (ostream& out,const term& object);
}
then in other file poly.cpp i have:
ostream & operator << (ostream& out, const Polynomial object){
term* q = object.getptr();
if (object.getptr() == NULL)
out << "( )";
else
while(q != NULL)
{
out << q->coef << "x^" << q->deg << " ";
q = q->next;
}
return out;
}
in main.cpp
Polynomial p5, then added some terms and cout << p5 but I get errors.
I think it's your declarations which are causing the problem:
friend ostream & operator << (ostream & out, const term & object);
and
ostream & operator << (ostream & out, const Polynomial & object);
These don't match. One is using a term object and the latter is using a Polynomial object. I'm assuming you want this function to use a term object because the function uses data memebers specific to the struct term. So change the latter to accept a term object:
ostream & operator << (ostream & out, const term & object);