Everywhere I look I see everyone overloading the << operator in a few lines, it seems very simple, but for some reason, overloading the << operator in my code does nothing.
In my .h I have:
friend std::ostream& operator<<(std::ostream& os, const Test& test);
And in my .cpp I have:
std::ostream& operator<<(std::ostream& out,const DeckOfCards& deck) {
out << "oi"; //just testing with a normal string before i try methods
return out;
}
And finally in the main function I have:
Test* test = new Test();
std::cout << "output is: " << test << std::endl;
Could someone please tell me what I'm doing wrong?
Thanks in advance.
How about trying this:
std::cout << "output is: " << *test << std::endl;
In your code, you are couting the pointer, not the object.
Related
I am really confused while overloading << operator as friend function.
This line works fine
cout << endl;
But this line gives compilation issue. Why ??
operator <<(cout, endl);
Below is the sample code
class Logger
{
int _id;
string _name;
public:
Logger(int id, string name):_id(id),_name(name){
cout<<"Constructor"<<endl;
}
~Logger(){
cout<<"destructor"<<endl;
}
friend ostream& operator <<( ostream& out,Logger& log);
};
ostream& operator << (ostream& out,Logger& log)
{
out<<log._id<<" "<<log._name;
return out;
}
And What is the need of return statment ? Without return also the below statment works fine.
cout<< log << endl << endl << log2 << endl << log3 << endl;
The operator<< that takes a stream manipulator is a member function of basic_ostream. You can't call it as if it were a free function; you have to call it as a member function:
std::cout.operator<<(std::endl)
On the other hand, the stream inserter that takes a std::string is a free function, and you can call it with the usual function call:
std::string text = "Hello, world";
operator<<(std::cout, text);
but not as a member function.
std::endl is usually the wrong thing to use; '\n' ends a line, without the extra stuff that std::endl does.
I am writing this code
ostream& operator <<(ostream& out, Box& B){
return B.l +" "+B.b +" "+B.h +endl;
};
The error I get is
Solution.cpp:40:46: error: ‘std::ostream& Box::operator<<(std::ostream&, Box&)’ must have exactly one argument ostream& operator <<(ostream& out, Box& B){ ^
can someone explain what's wrong? I dont understand.
thanks for your help :)
It seems you mean the following
std::ostream & operator <<( std::ostream& out, const Box& B) {
return out << B.l << " " << B.b << " " << B.h;
}
provided that all used in the operator data members are public data members of the class Box. The operator shall be declared and defined outside the class definition.
If one of the used data members is a private data member of the class then the function should be a friend function of the class and shall be declared (and may be defined) in the class definition. For example
class Box
{
//...
friend std::ostream & operator <<( std::ostream& out, const Box& B) {
return out << B.l << " " << B.b << " " << B.h;
}
//...
};
Pay attention to that it is better to remove in the return statement the operand std::endl. In this case 1) the operator will be more flexible because you can output additional information in the same line and 2) this statement
std::cout << box;
will not confuse readers of the code because they will not see the operand std::endl.
Without this operand in the operator definition in the caller of the operator you can write
std::cout << box << std::endl;
and this statement more clear expresses the intention of the programmer.
It should probably be:
std::ostream& operator<<(std::ostream& out, Box const& B){
out << B.l << " " << B.b << " " << B.h << std::endl;
return out;
};
Full code should look like:
#include <iostream>
class Box {
int l;
int b;
int h;
friend std::ostream& operator<<(std::ostream& out, Box const& B);
};
std::ostream& operator<<(std::ostream& out, Box const& B){
out << B.l << " " << B.b << " " << B.h << std::endl;
return out;
}
int main() {
return 0;
}
Demo
To output a type T to std::ostream, you have to declare standalone operator, which returns reference to that stream
std::ostream& operator<<(std::ostream& out, const T& B)
{
out << B.l << " " << B.b << " " << B.h << '\n';
return out;
}
This operation cannot be member operator because member operator use their class as first argument, therefore it might be necessary declare it as a friend of class T.
Avoid using endl in such operators if not extremely necessary, that manipulator calls out.flush(). If you want new line added, use new-line character.
I learnt how to do operator overloading of Stream Insertion Operator. But one doubt remains.
#include<iostream>
class INT
{
int i;
friend std::ostream& operator<<(std::ostream&,INT&);
public:
INT():i(100){}
};
std::ostream& operator<<(std::ostream& obj,INT & data)
{
obj<<data.i;
return obj;
}
int main()
{
INT obj;
std::cout<<obj;
}
What significance return obj; has?
Do that return have any use further?
Are We forced to do that return because of the syntax of the operator<< without any usefulness?
Remember how you can write code like this:
cout << "The data is: " << somedata << endl;
This is actually the same as:
((cout << "The data is: ") << somedata) << endl;
For this to work, the << operator has to return the stream.
I get the compiler error
no match for 'operator<<' in 'std::cout << VertexPriority(2, 4u)'
In the main class referred to this operator overloading, but I can't undestand where the error is.
Here there is the operator overloading line, I implemented it inside the class definition.
std::ostream& operator<<(std::ostream& out) const { return out << "Vertex: " << this->vertex << ", Priority: " << this->priority; }
vertex and priority are integer and unsigner integer.
In the main class I'm trying to doing this:
std::cout << VertexPriority(2, 3) << std::endl;
Define it like this:
class VertexPriority {
...
friend std::ostream& operator<< (std::ostream& out, const VertexPriority& vp);
};
std::ostream& operator<< (std::ostream& out, const VertexPriority& vp) {
return out << "Vertex: " << vp.vertex << ", Priority: " << vp.priority;
}
The friend keyword is necessary if VertexPriority::vertex or VertexPriority::priority are not public.
For more help, read this tutorial: http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/
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);
}