自定义类 ostringstream 输出错误 [英] Custom class ostringstream output error

查看:39
本文介绍了自定义类 ostringstream 输出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么会出错,但我只是想向 endl 添加一些类似"的东西,以便我可以将 ostringstream 中的内容扔给我们的调试器.我有以下几点:

I don't know why this is erroring, but I'm just trying to add something "akin" to endl so that I can throw what's in an ostringstream to our debugger. I have the following:

class debug_stream_info
{
public:
    debug_stream_info(int errorLine, char *errorFile, int level)
        :m_errorLine(errorLine), m_errorFile(errorFile), m_logLevel(level)
    {
    }

    friend std::basic_ostringstream<char>& operator<<(std::basic_ostringstream<char>& os, debug_stream_info& debug_info);

private:
    int m_errorLine;
    std::string m_errorFile;
    int m_logLevel;

};

std::basic_ostringstream<char>& operator<<(std::basic_ostringstream<char>& os, debug_stream_info& debug_info)
{
    // Write the stream's contents to cpu_debug
    // Deleted custom logging function.  No errors here though

    // Clear the stream for re-use.
    os.str("");
    os.seekp(0);

    return os;
}

int main(int argc, char** argv)
{
    std::ostringstream myout;
    myout << "hey there" << " and some more " << "Numbers!!: " << 435 << 54.2 << " that's good for numbers" << debug_stream_info(__LINE__, __FILE__, LOG_LEVEL);

    return 0;
}

我得到的错误是:error C2679: binary '<<': 没有找到在 main 中的行采用debug_stream_info"类型的右侧操作数(或没有可接受的转换) 的运算符.这是在 VS2008 上.

The error I'm getting is: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'debug_stream_info' (or there is no acceptable conversion) for the line in main. This is on VS2008.

我包括 sstream、iostream 等,并正确设置了命名空间.我没有收到其他错误.我什至尝试用 ostringstream 替换所有出现的 basic_ostream 并且没有区别(我稍后会有一个 w_char 版本,但我希望简单的案例首先起作用).我在上面一行做了对象,然后在行上传递了一个完全构造的对象,错误完全一样.我已经将第二个参数的签名更改为 const 和从 const 更改为好.

I'm including sstream, iostream, etc, and have the namespaces set up right. I'm getting no other errors. I even tried replacing all occurrances of basic_ostream with just ostringstream and there was no difference (I'll be having a w_char version later, but I wanted the simple case to work first). I made the object on the line above and then passed a fully-constructed object on the line, and the error was exactly the same. I've changed the signature of the second argument to and from const with no change as well.

对我在这里做错了什么有什么想法吗?

Any ideas on what I'm doing wrong here?

由于每个响应似乎都想把它放在那里,我不能使用 std::ostream 因为我希望它只适用于 std::ostringstream(和 std::basic_ostringstream) 而不是用于任何类型的输出流.此外,该函数无论如何都不会用 ostream 编译,因为我使用的是 os.str() 方法,它不在 ostream 中,只有子类.

since EVERY response seems to want to put it there, I can NOT use std::ostream because I want this to work ONLY for std::ostringstream (and std::basic_ostringstream) and not for any type of output stream. Besides, the function wouldn't compile with ostream anyways, since I'm using the os.str() method, which isn't in ostream, only the sub-classes.

推荐答案

你的代码的真正问题是你重载了 std::ostringstream 而不是 std::ostream.所以如果你这样写,你的代码就会工作:

The real problem with your code is that you've overloaded std::ostringstream rather than std::ostream. So your code would work if you write this:

debug_stream_info info(/** blah blah**/);

std::ostringstream oss;
oss << info ; //OK

但是这行不通:

oss << 1 << info; //ERROR

这是编译错误,因为表达式 oss<<1 返回类型为 std::ostream& 的对象,该对象没有采用 的重载debug_stream_info 作为第二个参数.这意味着如果你使用 cast as:

This is compilation error because the expression oss<<1 returns an object of type std::ostream& which doesn't have overload which takes debug_stream_info as second argument. That means if you use cast as:

static_cast<std::ostringstream&>(oss << 1) << info; //OK

然后应该可以再次工作.

then that should work again.

所以解决方案是重载std::ostream,而不是重载std::basic_ostringstream.

So the solution is to overload std::ostream, instead of std::basic_ostringstream.

另外,第二个参数应该是 const & .这也是你的代码的问题.

Also, the second parameter should be const & . This is also a problem with your code.

所以写这个:

std::ostream& operator<<(std::ostream&, debug_stream_info const &);
                                                        //^^^^^^^ note this

第二个参数应该是 const & 以便您可以将临时对象写入流.

The second parameter should be const & so that you could write temporary objects to the stream.

这篇关于自定义类 ostringstream 输出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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