为什么我的更改没有粘住?

发布于 2024-07-19 19:56:34 字数 1608 浏览 8 评论 0原文

我认为我无法理解 C++ 的一些细节。 我想设置一个日志来记录我的程序所做的事情,并发现了 std::clog,它在理论上似乎做了我想要的事情,但实际上却没有。

如果我执行以下操作,clog 将按预期工作并将“测试 1”写入屏幕,并且“测试 2”显示在文件中:

int main ()
{
    clog << "Test 1" << endl;
    streambuf* original_buffer = clog.rdbuf (ofstream ("test.log").rdbuf ()));
    clog << "test 2" << endl;

    clog.rdbuf (original_buffer);
    return 0;
}

但是如果我将所有这些放入这样的类中,则会写入“测试 1”在屏幕上,test.log 已创建,但里面没有任何内容,并且找不到“Test 2”!:

class IerrLog
{
    std::streambuf * original_buffer;
    public:
    IerrLog ()
    {
        std::ofstream logFile ("test.log");
        original_buffer = std::clog.rdbuf (logFile.rdbuf ());
    }
    ~IerrLog ()
    {
        std::clog.rdbuf (original_buffer);
    }
};

int main () {
    clog << "Test 1" << endl;
    IerrLog someLog ();
    clog << "Test 2" << endl;
    return 0;
}

我缺少什么?

编辑:如果我在 valgrind 中运行后者,我会得到这样的错误(前者运行干净):

Invalid read of size 8
    at 0x39598993E5: std::ostream::flush() (in /usr/lib/libstdc++.so.6.0.10)
    by 0x395989B5F2: std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&) (in /usr/lib/libstdc++.so.6.0.10)
    by 0x400F8E: main (main.cc:23)
  Address 0x7ff0006c8 is just below the stack ptr.  To suppress, use: --workaround-gcc296-bugs=yes

我还没有讨厌到认为我(一个低级的普通程序员)用这样一个简单的程序发现了一个编译器错误,但是这个让我更加困惑,valgrind 显然发现后者在某种程度上是错误的,尽管我试图使它们在功能上相同。

I think I'm failing to understand some finer point of C++. I want to set up a log of what my program does, and discovered std::clog, which seems to do what I want in theory, but in practice it doesn't.

If I do the following, clog works as expected and writes "Test 1" to the screen, and "Test 2" shows up in a file:

int main ()
{
    clog << "Test 1" << endl;
    streambuf* original_buffer = clog.rdbuf (ofstream ("test.log").rdbuf ()));
    clog << "test 2" << endl;

    clog.rdbuf (original_buffer);
    return 0;
}

But if I put all that into a class as such, then "Test 1" is written to the screen, test.log is created, but there's nothing inside and "Test 2" is no where to be found!:

class IerrLog
{
    std::streambuf * original_buffer;
    public:
    IerrLog ()
    {
        std::ofstream logFile ("test.log");
        original_buffer = std::clog.rdbuf (logFile.rdbuf ());
    }
    ~IerrLog ()
    {
        std::clog.rdbuf (original_buffer);
    }
};

int main () {
    clog << "Test 1" << endl;
    IerrLog someLog ();
    clog << "Test 2" << endl;
    return 0;
}

What am I missing?

EDIT: If I run the latter in valgrind, I get errors like this (the former runs clean):

Invalid read of size 8
    at 0x39598993E5: std::ostream::flush() (in /usr/lib/libstdc++.so.6.0.10)
    by 0x395989B5F2: std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&) (in /usr/lib/libstdc++.so.6.0.10)
    by 0x400F8E: main (main.cc:23)
  Address 0x7ff0006c8 is just below the stack ptr.  To suppress, use: --workaround-gcc296-bugs=yes

I'm not obnoxious enough to think that I (a lowly common programmer) found a compiler bug with such a simple program, but this makes me even more confused and valgrind obviously finds that the latter is somehow wrong, even though I tried to make them functionally identical.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虚拟世界 2024-07-26 19:56:34

我假设您想创建一个 IerrLog 堆栈变量。 您需要更改

IerrLog someLog ();

IerrLog someLog;

您的原始语句将被编译器解释为函数 someLog() 的声明,该函数不带参数并返回 IerrLog。

您还应该将文件创建为成员变量而不是在堆栈上。

I assume you want to create a stack variable of IerrLog. You need to change

IerrLog someLog ();

to

IerrLog someLog;

Your original statement will be interpreted by the compiler as a declaration of function someLog() which takes no arguments and returns an IerrLog.

You should also create your file as a member variable and not on the stack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文