How to cout the std::basic_string

前端 未结 3 1988
眼角桃花
眼角桃花 2020-12-16 14:41

I am trying to cout a basic_string. But cout is throwing error. Can I know how to do that

3条回答
  •  感动是毒
    2020-12-16 14:51

    As @Bo Persson mentioned, another way of defining a tcout type would be using references with the correct stream types. Though there are a few more things to consider when doing that, as you'll easily end up with linker issues due to multiple or missing definitions.

    What works for me is declaring these types as external references in a header and defining them once in a source file. This also works in a precompiled header (stdafx).

    Header

    namespace std
    {
    #ifdef UNICODE
        extern wostream& tcout;
    #else
        extern ostream& tcout;
    #endif // UNICODE
    }
    

    Implementation

    namespace std
    {
    #ifdef UNICODE
        wostream& tcout = wcout;
    #else
        ostream& tcout = cout;
    #endif // UNICODE
    }
    

提交回复
热议问题