[英]C++ friend Operator<<(ostream os, obj objName)
I have a question concerning the operator<< overload. 我有一个关于operator <<重载的问题。 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: 这是我的ColonneCartes.cpp类中的运算符:
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! 非常感谢!
The console you are refering to is called standard output ( stdout ). 您所引用的控制台称为标准输出( stdout )。 In C++, using streams to output to the stdout , you should use the
std::cout
object from iostream
header. 在C ++中,使用流输出到stdout ,应使用
iostream
标头中的std::cout
对象。
Suppose you have a colonneCartes
object of type ColonneCartes
, then, in order to output its myVector
to stdout , write: 假设你有一个
colonneCartes
类型的对象ColonneCartes
,然后,以输出其myVector
到stdout,写:
#include <iostream>
...
std::cout << colonneCartes;
The operator<<
you wrote is what enables objects of type ColonneCartes
to be used after the <<
. 该
operator<<
你写的,是使类型的对象ColonneCartes
后使用<<
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.