Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly overload the << operator for an ostream?

I am writing a small matrix library in C++ for matrix operations. However my compiler complains, where before it did not. This code was left on a shelf for 6 months and in between I upgraded my computer from debian etch to lenny (g++ (Debian 4.3.2-1.1) 4.3.2 ) however I have the same problem on a Ubuntu system with the same g++.

Here is the relevant part of my matrix class:

namespace Math {     class Matrix     {     public:          [...]          friend std::ostream& operator<< (std::ostream& stream, const Matrix& matrix);     } } 

And the "implementation":

using namespace Math;  std::ostream& Matrix::operator <<(std::ostream& stream, const Matrix& matrix) {      [...]  } 

This is the error given by the compiler:

matrix.cpp:459: error: 'std::ostream& Math::Matrix::operator<<(std::ostream&, const Math::Matrix&)' must take exactly one argument

I'm a bit confused by this error, but then again my C++ has gotten a bit rusty after doing lots of Java those 6 months. :-)

like image 495
Matthias van der Vlies Avatar asked Jan 24 '09 16:01

Matthias van der Vlies


People also ask

Can << operator be overloaded?

Output streams use the insertion ( << ) operator for standard types. You can also overload the << operator for your own classes.

What is the correct way to overload operator?

All operators in C#.NET can be overloaded. We can use the new modifier to modify a nested type if the nested type is hiding another type. In case of operator overloading all parameters must be of the different type than the class or struct that declares the operator.

Can we overload << operator in C++?

You can redefine or overload most of the built-in operators available in C++. Thus, a programmer can use operators with user-defined types as well. Overloaded operators are functions with special names: the keyword "operator" followed by the symbol for the operator being defined.

What is operator overloading explain the >> << operator with the help of example?

This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can overload an operator '+' in a class like String so that we can concatenate two strings by just using +.


1 Answers

Just telling you about one other possibility: I like using friend definitions for that:

namespace Math {     class Matrix     {     public:          [...]          friend std::ostream& operator<< (std::ostream& stream, const Matrix& matrix) {             [...]         }     }; } 

The function will be automatically targeted into the surrounding namespace Math (even though its definition appears within the scope of that class) but will not be visible unless you call operator<< with a Matrix object which will make argument dependent lookup find that operator definition. That can sometimes help with ambiguous calls, since it's invisible for argument types other than Matrix. When writing its definition, you can also refer directly to names defined in Matrix and to Matrix itself, without qualifying the name with some possibly long prefix and providing template parameters like Math::Matrix<TypeA, N>.

like image 181
Johannes Schaub - litb Avatar answered Sep 25 '22 02:09

Johannes Schaub - litb