I am trying to overload the ostream operator as a friend in a class to build components of a circuit, however it keeps returning the address.
In a series-circuit class in the file "Circuit_classes.h":
friend ostream& operator<< (ostream& os, series_circuit const& myCircuit);
In the file "Circuit_classes.cpp":
ostream& operator<<(ostream& os, series_circuit const& myCircuit){
os << "Output: " << myCircuit.frequency << endl;
return os;
}
where frequency is defined in the class header file as 60.
In my main program, "AC Circuits.cpp"
vector<shared_ptr<circuit>> circuit_vector;
circuit_vector.push_back(shared_ptr<circuit>(new series_circuit));
cout << circuit_vector[0] << endl;
Output in the command line when the program is run:
0325E180
cout << circuit_vector[0] << endl;
circuit_vector[0] yields a std::shared_ptr which is what is being printed.
You must dereference it to get to the object itself.
cout << *circuit_vector[0] << endl;
Related
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.
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.
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/
Is cout ever copied implicitly?
For example, is the cout object passed to the second overloaded operator in the code below, and the cout object inside its implementation are the same objects or is one a copy of cout?
My understanding is that the first implementation is correct because the << operator works for any ostream object, e.g. it will work for ofstream objects for writing to files.
//First implementation
ostream& operator<<(ostream& os, const Date& dt)
{
os << dt.mo << '/' << dt.da << '/' << dt.yr;
return os;
}
//Second implementation
ostream& operator<<(ostream& os, const Date& dt)
{
cout << dt.mo << '/' << dt.da << '/' << dt.yr;
return cout;
}
//using second implementation on object date
cout<<date;
Your example doesn't copy any ostream, you use references everywhere. If you look at std::cout you can see that it is noncopyable (here and here)
std::istream and std::ostream objects cannot be copied. Since std::cout is an ostream object (its type is derived from std::ostream), it cannot be copied.
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);
}