wstring CLuaProgramNode::GetLuaNodeString( int iId, wstring sValue)
{
wstring str; int iPropertyId = iId;               
wstring sPropertyValue = sValue;    if (sPropertyValue != "") // error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::wstring' (or there is no acceptable conversion)
{
wsprintf(str, L"{%d/%s}", iPropertyId, sPropertyValue); //error C2664: 'wsprintfW' : cannot convert parameter 1 from 'std::wstring' to 'LPWSTR'
} return str;
}这两个问题怎么解决呢???要求必须返回wstring,不能用CString中的Format函数来代替。求解决办法!!!!!!!!!!

解决方案 »

  1.   

     if(wcscmp(sPropertyValue.c_str(),L""))
      

  2.   

    wsprintf((LPWSTR)str.c_str() , L"{%d/%s}", iPropertyId, sPropertyValue.c_str());
      

  3.   


     if(*(sPropertyValue.c_str()))
      

  4.   

    if(sPropertyValue.c_str() != "")
    {
    wsprintf((LPWSTR)str.c_str() , L"{%d/%s}", iPropertyId, sPropertyValue.c_str()); //error C2664: 'wsprintfW' : cannot convert parameter 1 from 'std::wstring' to 'LPWSTR'
    }
      

  5.   

    第一个问题:
    把if (sPropertyValue != "") 改为:if (sPropertyValue != L"") 第二个问题:
    wstring 不能作为wsprintf的第一个参数,wsprintf的原型为:
    int wsprintf( 
      LPTSTR lpOut, 
      LPCTSTR lpFmt, 
      ...
    );
    你可以另外定义一个缓冲数组,格式化到里边以后,然后再把该格式化的串赋给str.
    wstring CTestzwpDlg::GetLuaNodeString( int iId, wstring sValue)
    {
        wstring str;    int iPropertyId = iId;               
        wstring sPropertyValue = sValue;   
    TCHAR buffer[256];    if (sPropertyValue != _T("")) // error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::wstring' (or there is no acceptable conversion)
        {
            wsprintf(buffer, _T("{%d/%s}"), iPropertyId, sPropertyValue); //error C2664: 'wsprintfW' : cannot convert parameter 1 from 'std::wstring' to 'LPWSTR'
        }
    str = buffer;
        return str;
    }
      

  6.   

    还有wstring的c_str方法理论上来讲是不能赋值给它的。c_str()是返回string(或wstring)内部存放字符串的缓冲区,以0结尾.但是因为是有string自己维护的,所以不许外部修改.
      

  7.   

    wstring CLuaProgramNode::GetLuaNodeString( int iId, wstring sValue)
    {
    wchar_t buffer[256] = {0};//使用buffer int iPropertyId = iId;               
    wstring sPropertyValue = sValue;    if (wcscmp(sPropertyValue.c_str(),L""))
    {
    wsprintf(buffer, L"{%d/%s}", iPropertyId, sPropertyValue.c_str()); 
    } return wstring(buffer);//转换一下
    }
      

  8.   

    上面犯了个错误,要考虑到unicode和多字节的情况,修改如下
    wstring CLuaProgramNode::GetLuaNodeString( int iId, wstring sValue) 

    wchar_t buffer[256] = {0};//使用buffer int iPropertyId = iId;              
    wstring sPropertyValue = sValue;  if (wcscmp(sPropertyValue.c_str(),L"")) 

    wsprintfW(buffer, L"{%d/%s}", iPropertyId, sPropertyValue.c_str()); //使用宽字节函数
    } return wstring(buffer);//转换一下 
    }
    另:5楼的代码也需要修改下。不可以使用_T,当用户在多字节的情况下,会变成
    if (sPropertyValue != "") 这很明显编译不过。