std :: ostream需要帮助的函数 [英] std::ostream need help with function

查看:251
本文介绍了std :: ostream需要帮助的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有人解释我这些代码行的一部分,我需要一些帮助使用ostream与简单的例子。谢谢:)。

  inline std :: ostream& operator<<<(std :: ostream& os,const Telegram& t)
{
os< time:<< t.DispatchTime< 发件人:< t.Sender
<< Receiver:<<接收器<< Msg:<< tsMsg;

return os;
}



更新1:当我使用这个函数时,它不编译和错误说:



std :: ostream& class :: operator<<

解决方案



当你添加一个新类,你想要输出流如 cout 来智能地处理它们,您需要添加一个新的<< 操作符方法,它具有新的对象类型作为第二个参数。



上面的代码正是这样做的。稍后执行语句时:

 电报tg(Bob,你好,你好吗? 
cout<< tg;

您的问题中的函数将调用流作为第一个参数,您的 tg 对象作为第二个参数,然后它将能够以适合该类的格式输出数据。



实际上是早期的C ++的东西,我有麻烦让我的头。虽然这个类应该是自包含的,但你实际上是在一个不同的类中添加一些东西来处理输出。一旦你明白为什么会发生这种情况(因为它是负责输出事物而不是你自己的类的 ostream 类),这将是有意义的。






希望使用更简单的例子更清楚:

  1 inline std :: ostream& operator<<<(std :: ostream& os,const Telegram& t){
2 os< message:< tsMsg;
3 return os;
4}

第1行只是函数定义。它允许你返回流本身(你传递),所以你可以链接<< 段。 运算符<< 只是您提供的函数,它是在您输入<



第2行使用更基本的<<



然后,第3行返回流,再次允许链接

code><<
段。



基本思想是提供运算符< / code>函数建立在构成类型的数据类型的现有运算符< 函数上。






和一个只包含 int 的简单包装类:

  #include< iostream> 

//这是我的简单类。

class intWrapper {
public:
intWrapper(int x){myInt = x; };
int getInt(void){return myInt; }
private:
int myInt;

//必须是访问私有成员的朋友。

friend std :: ostream&运算符<< (std :: ostream& amp; const intWrapper&);
};

//实际的输出函数。

inline std :: ostream&运算符<< (std :: ostream& os,const intWrapper& t){
os< int:< t.myInt;
return os;
}

//测试的主程序。
//输出getter和ostream。

int main(void){
class intWrapper x(7);
std :: cout<< x.getInt()<< std :: endl; // ostream已经知道int。
std :: cout<< x<< std :: endl; //以及intWrapper,由于上面声明的
//函数。
return 0;
}



此输出:

  7 
int:7

通过调用getter函数检索整数,第二个通过调用< 运算符函数添加到 ostream


i need someone who explain me these lines of code part by part and i need some help in using "ostream" with simple examples. thank you :).

inline std::ostream& operator<<(std::ostream& os, const Telegram& t)
{
  os << "time: " << t.DispatchTime << "  Sender: " << t.Sender
     << "   Receiver: " << t.Receiver << "   Msg: " << t.Msg;

  return os;
}

UPDATE 1: when i use this function it doesnt compile and the error says:

std::ostream& class::operator<<(std::ostream& os, const Telegram& t) must take exactly one argument

解决方案

These line are simply adding the ability to handle Telegram objects to the standard output stream class.

When you add a new class and you want output streams like cout to intelligently handle them, you need to add a new << operator method which has the new object type as the second argument.

What the code above is doing is exactly that. When you later execute the statements:

Telegram tg("Bob", "Hello, how are you?");
cout << tg;

that function in your question will be called with the stream as the first argument and your tg object as the second argument and it will then be able to output the data in a format suitable to the class.

This was actually one of the early C++ things I had trouble getting my head around. Although the class is supposed to be self-contained, you're actually adding something to a different class to handle the output. Once you understand why this is happening (because it's the ostream class that is responsible for outputting things rather than your own class), it will hopefully make sense.


Hopefully making it clearer with a simpler example:

1    inline std::ostream& operator<<(std::ostream& os, const Telegram& t) {
2      os << "message: " << t.Msg;
3      return os;
4    }

Line 1 is simply the function definition. It allows you to return the stream itself (that you pass in) so you can chain << segments. The operator<< is simply the function you're providing, which is the one called when you put << tg into an output stream statement.

Line 2 uses more basic << statements that have already been defined (in this case, whatever type Msg is, probably a string).

Then line 3 returns the stream, again to allow chaining of << segments.

The basic idea is to provide operator<< functions that build on existing operator<< function for the data types that constitute your type.


And with a simple wrapper class containing just an int:

#include <iostream>

// This is my simple class.

class intWrapper {
    public:
        intWrapper (int x) { myInt = x; };
        int getInt (void) { return myInt; }
    private:
        int myInt;

    // Must be friend to access private members.

    friend std::ostream& operator<< (std::ostream&, const intWrapper&);
};

// The actual output function.

inline std::ostream& operator<< (std::ostream& os, const intWrapper& t) {
    os << "int: " << t.myInt;
    return os;
}

// Main program for testing.
// Output with getter and with ostream.

int main (void) {
    class intWrapper x(7);
    std::cout << x.getInt() << std::endl;   // ostream already knows about int.
    std::cout << x << std::endl;            // And also intWrapper, due to the
                                            //   function declared above.
    return 0;
}

This outputs:

7
int: 7

the first by just calling the getter function to retrieve the integer, the second by calling the << operator function we added to ostream.

这篇关于std :: ostream需要帮助的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆