BOOST unittest override operator<< - c++

To test two std::pair
BOOST_CHECK_EQUAL(std::make_pair(0.0,0.0), std::make_pair(1.0,1.0));
I overloads the operator<< for std::pair
std::ostream& operator<< (std::ostream& os, const std::pair<double,double>& t)
{
return os << "( " << t.first << ", " << t.second << ")";
}
with following error
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::pair<_Ty1,_Ty2>' (or there is no acceptable conversion)
What is wrong?

Open up the std namespace so ADL can find it.
namespace std
{
ostream& operator<< (ostream& os, const pair<double,double>& t)
{
return os << "( " << t.first << ", " << t.second << ")";
}
}
Ok, I figured it out. Name lookup stops when it finds the name it is looking for in the current namespace, which is why it cannot find your operator<< in global scope, since it found operator<< already in namespace boost because boost declares operator<<.
I recommend reading Why can't I instantiate operator<<(ostream&, vector&) with T=vector? which has a good explanation.

Related

overloading operator << c++, I am trying to cout the element of class

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.

std::cout don't work with overloaded '<<' operator for struct

I've implemented operator '<<' overloading for struct LevelStats , which seems to work well with files , but encounter problem when used with std::cout
header file:
struct LevelStats
{
DIFFICULTY level;
std::chrono::duration<double> best_time;
unsigned int games_played;
unsigned int games_won;
};
std::ofstream& operator<<(std::ofstream &os, const LevelStats &stats);
cpp file:
std::ofstream &operator<<(std::ofstream &os, const LevelStats &stats) {
os << static_cast<unsigned int>(stats.level) << " " << "Best_Time= " << stats.best_time.count()<<std::endl;
os << static_cast<unsigned int>(stats.level) << " " << "Games_Played= " << stats.games_played<<std::endl;
os << static_cast<unsigned int>(stats.level) << " " << "Games_Won= " << stats.games_won<<std::endl;
return os;
}
That works well with for operations like
file << LevelStats object
, but when used as
std::cout << LevelStats object
results with :
error: cannot bind 'std::ostream {aka std::basic_ostream}'
lvalue to 'std::basic_ostream&&'
Edit: Replaced with std::ostream& meets the same error
Another Edit : Dumb mistake in arguments - it works
Your operator<< is declared as
std::ofstream& operator<<(std::ofstream &os, const LevelStats &stats);
Notice that you're passing and returning a reference to std::ofstream.
Writing to file will work because you will pass a std::ofstream&, but std::cout is not a std::ofstream& and can not bind to std::ofstream&.
If you want to be able to output your struct using std::cout while still beeing able to use std::ofstream, change your operator<< to
std::ostream& operator<<(std::ostream &os, const LevelStats &stats);
Both std::ofstream and std::ostream can bind to std::ostream &os, allowing you to write your struct to both files and std::cout.

Operator overloading << error

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/

<< operator overloading as a class method in c++

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);
}

Getting the compiler to perceive << as defined for a specific class

I edited a post of mine with this question, yet got no answers.
I overloaded << for a class, Score (defined in score.h), in score.cpp.
ostream& operator<< (ostream & os, const Score & right)
{
os << right.getPoints() << " " << right.scoreGetName();
return os;
}
(getPoints fetches an int attribute, getName a string one)
I get this compiling error for a test in main(), contained in main.cpp
binary '<<' : no operator found which takes a right-hand operand of type 'Score' (or there is no acceptable conversion)
How come the compiler doesn't 'recognize' that overload as valid? (includes are proper)
Thanks for your time.
EDIT:
As requested, code causing the error:
cout << ":::::\n" << jogador.getScore() << endl;
jogador is a Player object, which contains a Score one. getScore returns that attribute.
Perhaps you didn't declare your operator<< in score.h? It should normally contain something like:
ostream& operator<< (ostream & os, const Score & right);
Edit: More accurately, that should be:
std::ostream &operator<<(std::ostream &os, const Score &right);
You definitely should not have a using namespace std; in a header, so you need the std:: for it to work correctly.
Try declaring operator<< as a friend function in your class:
struct Score
{
friend ostream& operator<<(ostream& output, const Score& right);
};
This will allow your Score structure to fit nicely into printing statements:
Score my_score;
cout << my_score << endl;
When in doubt, check the C++ FAQ.