开发者

calling std::cout.rdbuf() produces syntax error

Maybe I missed something, but I cant figure out why Visual Studio 2008 isn't seeing the rdbuf() procedure. Here is my code:

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

The BufferedStringBuf class is from this page: http://www.devmaster.net/forums/showthread.php?t=7037

Which produces the following error:

...src\ma开发者_运维技巧in.cpp(19) : error C2143: syntax error : missing ';' before '.'

All I want to do is redirect std::cout to the Visual Studio Output window using OutputDebugString()..


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.


Using the example class given on that site i don't have any problem:

#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);
}

Note that you have to create an instance of a class that derives from BufferedStringBuf because BufferedStringBuf::writeString() is pure virtual, thus making it an abstract class - abstract classes can't be instantiated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜