0

也许我错过了一些东西,但我不明白为什么 Visual Studio 2008 没有看到 rdbuf() 过程。这是我的代码:

16. #include "DebugBuffer/BufferedStringBuf.h"
17.
18. BufferedStringBuf debug_buffer(256);
19. std::cout.rdbuf(&debug_buffer);

BufferedStringBuf 类来自这个页面:http ://www.devmaster.net/forums/showthread.php?t=7037

这会产生以下错误:

...src\main.cpp(19) : error C2143: syntax error : missing ';' before '.'

我要做的就是使用 OutputDebugString() 将 std::cout 重定向到 Visual Studio 输出窗口。

4

2 回答 2

4

You're not allowed to have executable statements at file-level scope. You can declare variables, but you can't call functions as standalone statements. Move your code into a function (such as gf's answer demonstrates), and you should have no problem.

于 2010-04-10T16:56:22.297 回答
1

使用该站点上给出的示例类我没有任何问题:

#include <iostream>
#include "DebugBuffer/BufferedStringBuf.h"

class DbgBuf : public BufferedStringBuf {
public:
    DbgBuf() : BufferedStringBuf(255) {}
    virtual void writeString(const std::string &str) {}
};

int main()
{
    DbgBuf debug_buffer;
    std::cout.rdbuf(&debug_buffer);
}

请注意,您必须创建一个派生自的类的实例,BufferedStringBuf因为BufferedStringBuf::writeString()它是纯虚拟的,因此使其成为抽象类 - 无法实例化抽象类。

于 2010-04-10T16:25:44.420 回答