compilation error when I overload cout operator inside the class

operator overloading
overloading operator c++
overloading operator c++
ostream operator overloading c++ without friend
overloading the function operator
which operator cannot be overloaded in java
which of the following operator can be overloaded
overloaded 'operator<<' must be a binary operator (has 3 parameters)

compilation error when I overload cout operator inside the class

What I am missing?

Here after the source code. The problem disappear when I define the overload operator outside the class

#include <iostream>
using namespace std;

class Box {
    public:
                int l, b, h;
        Box(int length, int breadth, int height) : l(length), b(breadth), h(height) {}
#if 1
        ostream& operator<<(ostream& os) {
            os << (l * b * h);
            return os;
        }
#endif
};

#if 0
ostream& operator<<(ostream& os, Box inb) {
    os << (inb.l * inb.b * inb.h);
    return os;
}
#endif


int main(void) {
    Box B(3,4,5);
        cout << B << endl;
    return 0;
}

The member function:

ostream& operator<<(ostream& os);

would be useful in this case:

boxobject << os;

which is rarely what you want to do. Instead, you need this free function:

std::ostream& operator<<(std::ostream& os, const Box& inb) {
    os << (inb.l * inb.b * inb.h);
    return os;
}

9.3 — Overloading the I/O operators, Because print() returns void, it can't be called in the middle of an output statement . Instead, you Here is the above Point class with the overloaded operator<<. However, if you try to return std::ostream by value, you'll get a compiler error. Date dt(1, 2, 92); cout <<dt; To get cout to accept a Date object after the insertion operator, overload the insertion operator to recognize an ostream object on the left and a Date on the right. The overloaded << operator function must then be declared as a friend of class Date so it can access the private data within a Date object.

The expression X << Y (when at least one operand has class or enum type) looks for whether operator<<(X, Y) or X.operator(Y) can be called.

So in cout << B, it looks for operator<<(cout, B) and cout.operator<<(B). Notice it's not at all looking for members of B. It looks for members of cout, but you can't add new members to a standard library class. So you need to define the non-member function.

The member you did define would make B << std::cout << std::endl; work, but that doesn't look right, obviously.

One other option, necessary if the operator needs to use private members of the class, or nice if we just want the operator visible in the class's public section, is to declare it a friend, which is really a non-member:

class Box {
public:
    Box(int length, int breadth, int height) : l(length), b(breadth), h(height) {}

    friend std::ostream& operator<<(ostream& os, const Box& box) {
        os << (box.l * box.b * box.h);
        return os;
    }

private:
    int l, b, h;
};

Operator Overloading, C++ FAQ, But operator overloading makes my class look ugly; isn't it supposed to make my code class Array {; public: int& elem(unsigned i) { if (i > 99) error(); return data[i ]; } x ** y is the same as x * (*y) (in other words, the compiler assumes y is a pointer). For example, although C++ uses std::cout << x for printing, and although� In C++, stream insertion operator “<<” is used for output and extraction operator “>>” is used for input. We must know following things before we start overloading these operators. 1) cout is an object of ostream class and cin is an object istream class 2) These operators must be overloaded as a global function. And if we want to allow

You can't overload operator << that way and have it work like normal. When you have

ostream& operator<<(ostream& os) {
    os << (l * b * h);
    return os;
}

What you really have is

ostream& operator<<(Box& this_, ostream& os) {
    os << (this_.l * this_.b * this_.h);
    return os;
}

Which means you would need to call cout like

B << cout << endl;

The reason for this is that all class member functions have an implicit parameter of a reference type to the class as their first parameter. The compiler inserts this for you as that is how the member function knows which object to work on.

Operator Overloading and "This", Operator Overloading allows you to redefine the functioning of certain operators, //ADD for (i=0; i<MAXARRAY; i++) array3[i]=array1[i] + array2[i]; //PRINT cout Therefore, we have to define a class with an array and some member functions. In the overloaded operator case however, the compiler is smart enough to� Overloading operator>> It is also possible to overload the input operator. This is done in a manner analogous to overloading the output operator. The key thing you need to know is that std::cin is an object of type std::istream. Here’s our Point class with an overloaded operator>>:

Operator Overloading in C++ - GeeksQuiz, How does C++ compiler differs between overloaded postfix and prefix operators? A. C++ doesn't allow both operators to be overlaoded in a class. This section contains the C++ find output programs with their explanations on C++ Operator Overloading (set 2).

C++ Tutorial: Operator Overloading I - 2020, We must use the function prototype because the compiler looks at the In other words, operator overloading can be very useful to make our class look and getImag() << "i" << endl; c2 = c1; cout << "assign c1 to c2:" << endl; cout << "c2= " << c2. However, the v[4] is just a value, not a variable as indicated by the error. C++ is able to input and output the built-in data types using the stream extraction operator >> and the stream insertion operator <<. The stream insertion and stream extraction operators also can be overloaded to perform input and output for user-defined types like an object. Here, it is important

Operator Overloading, C++ allows almost all operators to be overloaded to apply to class objects. For example, the following code shows some operators (in red) being applied to IntList L1, L2, L3; L1 += 10; L3 = L1 + L2; cout << L1; if (L1[0] == 5) if (L1 == L2) . define these operators to apply to your class, you will get a compile-time error if� C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively. An overloaded declaration is a declaration that is declared with the same name as a previously declared declaration in

Comments
  • That is not how to overload the operator<< in either case. See here
  • does not compile. but if I add friend keyword then the compilation works fne
  • @MOHAMED If that's the case, your real Box definition is not like what you put in the question. If you use the Box definition as it is in the question, it works without friend: godbolt.org/z/e9zdhX - But sure, making operator<< a friend is common - or if you already have public accessors for l, b and h you can use those in operator<< if you don't want to make operator<< a friend.
  • the variables are public in my class. I used your function but still does not work. I added friend keyword and the compilation work now
  • @MOHAMED I don't understand your comment (or the comment you made to my answer). This answer already makes it a friend. Where did you add the friend keyword to this answer exacly?