2011-12-13 43 views
0

我正在創建一個VC++ 2008 Windows窗體應用程序,它需要使用我們的VC6項目中的一些類。將代碼從VC++ 6移動到VC++ 2008時出錯

當我增加了一個文件,其中包含下面的方法:

bool Property::createPaths(string &sPaths) 
{ 
    char *tok = NULL; 
    char seps[] = "\\"; 
    string str; 
    if (sPaths.size() > 0) 
    { 
     tok = strtok((char*)sPaths.c_str(),seps); 
     str = tok; 
     while (tok != NULL) 
     { 
      int res = CreateDirectory(str.c_str(),NULL); 
      tok = strtok(NULL,seps); 
      if (tok != NULL) 
      { 
       str += "\\"; 
       str += tok; 
      } 
     } 
     return true; 
    } 
    return false; 
} 

我得到錯誤抱怨CreateDirectory電話:

* 錯誤C2664: 'CreateDirectory':不能從轉換參數1 'const char '至'LPCTSTR'

在線搜索,似乎我需要一些配置配給我的VC2008項目來解決這個問題。任何人都可以告訴我在哪裏以及如何?

回答

3

您正將一個const char*傳遞給期望TCHAR*的函數。

TCHAR被定義爲charwchar_t取決於編譯設置 - ,默認情況下在VC2008它是wchar_t。您使用std::string時假設TCHARchar,這會導致您看到的錯誤。

有兩種方法供您合理的修正:

  • 在您的項目設置,更改Configuration Properties/General/Character SetUse Multi-Byte Character Set

或者

  • 重構代碼,以佔TCHAR潛在不同的定義 - 你會用std::basic_string<TCHAR>更換任何使用std::stringstd::wstring(使用適當的typedef)啓動此,敷串文字中的_T_TEXT宏。
+0

+1,十分鐘沒有回答,所以我寫了一個你打敗了我。 –