简体   繁体   English

继承自std :: ostringstream

[英]Inherit from std::ostringstream

In my project I use a class class called Message which inherits from std::ostringstream to print out human readable information of other class types. 在我的项目中,我使用一个名为Message的类,它继承自std :: ostringstream,打印出其他类类型的人类可读信息。

So its << operator is overloaded several times taking my own class types which I want to print out. 所以它的<<运算符多次重载我自己的类类型,我想打印出来。

class Message : public ostringstream
{
public:
    Message() {};
    virtual ~Message() {};

    Message& operator <<(const MyTypeA &a) { return *this << a.getStr(); };
    Message& operator <<(const MyTypeB &b) { return *this << b.identifier(); };
    Message& operator <<(const MyTypeC &c) { return *this << c.foo(); };

    // Pass everything unknown down to ostringstream, and always return a Message&
    template<class T>
    Message& operator <<(const T &t)
    {
        (std::ostringstream&)(*this) << t;
        return *this;
    }
};

Without the template 没有模板

MyTypeA a,b;
Message m;
m << a << "-" << b;

the code would not compile, as (m << a << "-") would return a ostringstream& which would not be able to take 'b'. 代码不会编译,因为(m << a <<“ - ”)将返回一个ostringstream并且无法取'b'。 So I use the template to make sure to always return a Message&. 所以我使用模板确保始终返回Message&。

My Problem: 我的问题:

Message m;
m << "Hello, world" << std::endl;

generates a veeery long compiler error and I do not know why. 生成一个veeery长编译器错误,我不知道为什么。

Here is a minimalistic "not" compilable example of my problem, and here is the corresponding compiler output. 是我的问题的简约“不”可编译示例, 这里是相应的编译器输出。

Do not derive from any of the stream classes to create a new stream! 不要从任何流类派生来创建一个新的流! The only reason to derive from std::ostream or std::istream is to create a stream properly set up to use a suitable stream buffer. std::ostreamstd::istream派生的唯一原因是创建一个正确设置的流以使用合适的流缓冲区。 Instead of deriving from a stream, you should derive from std::streambuf to create a new source or destination. 您应该从std::streambuf派生来创建新的源或目标,而不是从std::streambuf To create output operators for new types you'd overload operator<<() with std::ostream& as first argument and your custom type as second argument. 要为新类型创建输出运算符,您需要使用std::ostream&作为第一个参数并将自定义类型作为第二个参数重载operator<<() Likewise with std::istream& . std::istream&

BTW, the problem you encountered is because std::endl is a function template. 顺便说一句,你遇到的问题是因为std::endl是一个函数模板。 Trying to pass a function template somewhere requires that the appropriate instantiation can be deduced. 尝试在某处传递函数模板需要可以推导出适当的实例化。 Since your output operators don't cover a suitable signature, the instantiation of std::endl can't be deduced. 由于输出运算符不包含合适的签名,因此无法推导出std::endl的实例化。 I could state what's needed but that would be pointless as it is the wrong way to go anyway. 我可以说明需要什么,但这是毫无意义的,因为无论如何都是错误的方式。

Here is how to make your code compile: http://pastebin.com/xAt5junf 以下是如何编译代码: http//pastebin.com/xAt5junf

The relevant excerpt: 相关摘录:

class Message : public ostringstream
{
public:
    Message() {};
    virtual ~Message() {};
};

ostream& operator <<(ostream &os, const MyTypeA &a) {
    return os << a.getStr();
};

ostream& operator <<(ostream &os, const MyTypeB &b) {
    return os << b.getStr();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM