为什么我不能在Visual Studio的调试监视窗口中查看cout?

时间:2018-06-29 13:16:43

标签: c++ visual-studio

当我在VS中运行此代码时:

#include <iostream>
#include <sstream>
using namespace std;
int main(int args, char *argv[])
{
    ostringstream local;
    auto cout_buff = cout.rdbuf();
    cout.rdbuf(local.rdbuf());
    cout << "some message";
    cout.rdbuf(cout_buff);
    cout << "back to default buffer\n";
    cout << "local contetn:" << local.str() << endl;
} 

我想了解cout的内部变化,试图在 Watch Window 中添加cout,但是我得到了identifier "cout" is undefined。怎么会来?

enter image description here

1 个答案:

答案 0 :(得分:3)

我不得不推测为什么找不到cout,但这是创建本地别名和 watch 的解决方法:

int main(int args, char *argv[])
{
    auto& cout = std::cout;
    // ...