小编jqc*_*gel的帖子

Visual Studio 2012中的C++编译错误:LPCWSTR和wstring

以下代码在Visual Studio 2010中编译,但无法在Visual Studio 2012 RC中编译.

#include <string>

// Windows stuffs
typedef __nullterminated const wchar_t *LPCWSTR;

class CTestObj {
public:
    CTestObj() {m_tmp = L"default";};

    operator LPCWSTR()  { return m_tmp.c_str(); }       // returns const wchar_t*
    operator std::wstring() const { return m_tmp; }     // returns std::wstring

protected:
    std::wstring m_tmp;
};


int _tmain(int argc, _TCHAR* argv[])
{
    CTestObj x;
    std::wstring strval = (std::wstring) x;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

返回的错误是:

错误C2440:'type cast':无法转换'CTestObj''std::wstring'
No构造函数可以采用源类型,或构造函数重载解析不明确

我已经意识到,注释掉任何转换运算符都会修复编译问题.我只想了解:

  1. 引擎盖下发生了什么
  2. 为什么这个在VS2010而不是在VS2012中编译?是因为C++ 11的改变吗?

c++ visual-studio-2010 wstring visual-c++ visual-studio-2012

6
推荐指数
1
解决办法
1608
查看次数