SkAshe
SkAshe

Reputation: 3

C++ friend Operator<<(ostream os, obj objName)

I have a question concerning the operator<< overload. I need to use one in my homework to return the content of a vector. I think that my operator method works, however I have completely no idea how to call it in another class.

Here's my operator in my ColonneCartes.cpp class:

ostream& operator<<(ostream & os, const ColonneCartes & p_colonneCartes)
{
     for (int i = 0; i < myVector.size(); i++)
     {
          os << myVector.at(i).getValue();
     }

     return os;
};

I'm trying to call it from another class to show it in the console and I didn't find out how to do it yet.

My second question is about returning in my operator the content of a vector object and I wanted to know if there's a another way than using a loop like this one to return the content?

Thanks a lot!

Upvotes: 0

Views: 99

Answers (1)

Andr&#233; Sassi
Andr&#233; Sassi

Reputation: 1086

The console you are refering to is called standard output (stdout). In C++, using streams to output to the stdout, you should use the std::cout object from iostream header.

Suppose you have a colonneCartes object of type ColonneCartes, then, in order to output its myVector to stdout, write:

#include <iostream>
...
std::cout << colonneCartes;

The operator<< you wrote is what enables objects of type ColonneCartes to be used after the <<.

Upvotes: 1

Related Questions