Ostream overloading operator on a linked list - c++

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

Related

How to overload the '+' and '<<' operator in myself class

I create the class 'Point', overload the operator '+' between 2 Point object and the operator '<<' to show the Point object. I can't compile and run the code.
The error is that there is no operator "<<" matched. This is occured to "cout << "p3: " << (p1+p2) << endl;"
class Point {
public:
Point(int x=0, int y=0) : _x(x), _y(y) {};
Point operator +(Point &p);
int getX() {
return _x;
}
int getY() {
return _y;
}
friend ostream& operator <<(ostream& out, Point &p);
private:
int _x, _y;
};
Point Point::operator +(Point &p) {
Point np(this->_x+p.getX(), this->_y+p.getY());
return np;
}
ostream& operator <<(ostream &out, Point &p) {
out << '(' << p._x << ',' << p._y << ')';
return out;
}
int main() {
Point p1(1, 2);
Point p2;
cout << "p1: " << p1 << endl;
cout << "p2: " << p2 << endl;
cout << "p3: " << (p1+p2) << endl;
system("pause");
return 0;
}
The expression
(p1+p2)
is an rvalue. The function
ostream& operator <<(ostream &out, Point &p)
expects a reference to Point. You can't pass an rvalue to this function. Change it to
ostream& operator <<(ostream &out, const Point &p)
in the declaration and the definition.
C++ only allows a temporary to be passed to a const reference. See this: How come a non-const reference cannot bind to a temporary object?
Modify a temporary is meaningless. You need to define a const reference to promise you won't modify it and extend its lifetime.
When overloading operators, const correctness is not optional. The following prototypes are what you require...
Point operator + (const Point &p) const;
friend ostream& operator <<(ostream& out, const Point &p);

How to overload the << operator

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!

Problems with ostream

I'm working in a Big Integer implementation in C++ and I'm trying to use cout with my BigInt class. I already overloaded the << operator but it doesn't work in some cases.
Here is my code:
inline std::ostream& operator << (ostream &stream, BigInt &B){
if (!B.getSign()){
stream << '-';
}
stream << B.getNumber();
return stream;
}
The code above works with:
c = a + b;
cout << c << endl;
But fails with:
cout << a + b << endl;
In the first case the program runs fine, but in the second the compiler gave an error:
main.cc: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
It's possible to overload the << operator for function in both cases?
Methods:
string getNumber ();
bool getSign ();
string BigInt::getNumber (){
return this->number;
}
bool BigInt::getSign (){
return this->sign;
}
As chris already pointed out in comments very quickly (as usual), you have a temporary created in here:
cout << a + b << endl;
You cannot bind that to a non-const reference. You will need to change the signature of your operator overloading by adding the const keyword to the reference.
This code works for me with a dummy BigInt implementation (as you have not shared yours):
#include <iostream>
using namespace std;
class BigInt
{
public:
bool getSign() const { return true; }
int getNumber() const { return 0; }
const BigInt operator+(const BigInt &other) const {}
};
inline std::ostream& operator << (ostream &stream, const BigInt &B){
// ^^^^^
if (!B.getSign()){
stream << '-';
}
stream << B.getNumber();
return stream;
}
int main()
{
BigInt a, b, c;
c = a + b;
cout << c << endl;
cout << a + b << endl;
return 0;
}
But yeah, I agree that the error message is not self-explanatory in this particular case.
Change
inline std::ostream& operator << (ostream &stream, BigInt &B){
to
inline std::ostream& operator << (ostream &stream, BigInt const& B){
c can be a used where BiInt& is expected but a+b cannot be because a+b is a temporary. But it can be used where BigInt const& is expected.

Fixing "this" error in c++

I'm still new to c++ and I was wondering why I keep getting the error "invalid use of 'this' in non-member function" for every instance of "this" in my cpp file.
cpp file (just method)
ostream & operator<< (ostream & outS, Complex & rhs) {
cout << this->real;
if (this->imag < 0) {
cout << "-";
if (this->imag != -1)
cout << (-this->imag);
cout << "i";
} else if (this->imag >0){
cout << "+";
if (this->imag != 1)
cout << this->imag;
cout << "i";
return outS;
}
header file (part of)
public:
friend ostream & operator<< (ostream&, Complex&);
I also seem to be getting the error "'Complx' does not name a type
Complx::Complex (const Complex& object)"
^
cpp file
Complx::Complex (const Complex& object) {
real = object.real;
imag = object.imag;
}
header file
public:
Complex (const Complex&);
Any help would be much appreciated, and I can post the rest of my code if needed (I figured just posting parts of it would be easier to read).
this refers to your current object -- the object that the method is a part of. When your method stands alone, it's not part of an object so this has no meaning. It appears that instead of this-> you intend to refer to rhs..
In your operator<<, it is NOT a member of your class. Thus, it does not have this pointer, which is only for class non-static members.
class Complex
{
double imag, real;
public:
Complex(const _real=0.0, const _imag=0.0);
Complex(const Complex&);
// friend functions are not member of the class
friend ostream& operator<< (ostream&, Complex&);
// this is a member of the class
Complex operator+(Complex& another)
{
// I am a member so I have 'this' pointer
Complex result;
result.real = this->real + another.real;
result.imag = this->imag + another.imag;
return result;
}
};
ostream& operator<<(ostream& stream, Complex& rhs)
{
// I do not have 'this' pointer, because I am not a member of a class
// I have to access the values via the object, i.e. rhs
stream << rhs.real << "+" << rhs.imag << 'i';
return stream;
}
Yet my question is, why do you want to use 'friend operator<<'? IMHO it should not be a friend of the class, instead, the class should provide functions like double image() const { return this->imag; }, and the non-friend operator<< can access the values via these functions.

How to override the ++ operator in C++ and then print the output using an overriden << operator?

I'm trying to learn overriding operators in C++. But I'm stuck with this:
..\src\application.cpp: In function `int main()':
..\src\application.cpp:29: error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits](((std::basic_ostream >&)(&std::cout)), ((const char*)"Poly A: ")) << (&A)->Poly::operator++(0)'
Here's the line causing the error, It seems that my postincrement operator isn't returning anything printable:
cout << "Poly A: " << A++ << endl;
I have a Poly.h and a Poly.cpp file:
class Poly{
friend istream& operator>>(istream &in, Poly &robject);
friend ostream& operator<<(ostream &out, Poly &robject);
public:
Poly();
Poly operator++(int);
Poly operator++();
private:
int data[2];
};
Poly.cpp:
Poly Poly::operator++ (){
data[0]+=1;
data[1]+=1;
return *this;}
Poly Poly::operator++ (int){
Poly result(data[0], data[1]);
++(*this);
return result;
}
ostream& operator<<(ostream &out, Poly &robject){
out << "(" << robject.data[0] << ", " << robject.data[1] << ")";
return out;
}
I think the problem is that you declare your parameters as references:
ostream& operator<<(ostream &out, Poly &robject)
The reference will not bind to the temporaries that you return from your operator++.
If you make the Poly parameter a const reference you should be able to output it.