2011-07-12 88 views
0

我沒有寫下面的代碼,它是開源的,不再支持。我需要對它進行一些更改,所以我安裝了VC++ Express 2010,並且已經設法解決了大部分問題,並且該應用程序實際上是爲Windows編寫的,因此儘管它已經被頭髮拉動,但相當困難,我正在取得一些進展。需要連接std:string + WCHAR將gcc代碼移動到Visual C++ 2010

我被困在一個轉換,我還沒有完全得到關於VC++ 2010類型轉換手柄,但這裏是代碼給了我最後的頭痛...

路徑的std :: string和cFileName是WCHAR。我相信我成功地將路徑轉換爲WCHAR,但是mEntries.push_back遇到了問題。我需要做的是將cFileName轉換爲一個字符串,並且我已經搜索並嘗試了許多不同的方法來做到這一點,但是我得到的語法錯誤,或者它不能在VC++中工作,或者我完全錯過了其他的東西。

很高興知道它爲什麼不起作用,爲什麼會有很多不同的「字符串」類型(我寫的SQL腳本,這很荒謬),但在這一點上,我只需要幫助使它工作。

// Add files matching file spec to listing, returning number added. Each 
// filename is prepended with its path, if one was supplied in file spec. 

unsigned int DirList :: Add(const string & fspec) { 

// save path, if any 
STRPOS lastslash = fspec.find_last_of("\\/"); 
string path = lastslash == STRNPOS ? "" : fspec.substr(0, lastslash + 1); 
unsigned int count = 0; 
WIN32_FIND_DATA fd; 
HANDLE h = FindFirstFile(LPCWSTR(fspec.c_str()), & fd); 
if (h == INVALID_HANDLE_VALUE) { 
    return count; 
} 

do { 
    mEntries.push_back(
     new WinDirEntry(
       path + fd.cFileName, // add path back on 
       fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY 
     ) 
    ); 
    count++; 
} while(FindNextFile(h, & fd)); 

FindClose(h); 

return count; 
} 

的錯誤信息是:

error C2782: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(std::basic_string<_Elem,_Traits,_Alloc> &&,const _Elem *)' : template parameter '_Elem' is ambiguous 
1>   ...\microsoft visual studio 10.0\vc\include\string(143) : see declaration of 'std::operator +' 
1>   could be 'WCHAR' 
1>   or  'char' 

error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(std::basic_string<_Elem,_Traits,_Alloc> &&,std::basic_string<_Elem,_Traits,_Alloc> &&)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &&' from 'WCHAR [260]' 
1> ...\microsoft visual studio 10.0\vc\include\string(109) : see declaration of 'std::operator +' 

error C2676: binary '+' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator 
+1

一個快速的解決方案是使用ansi版本的Find *'。這是明確使用'FindFirstFileA'。否則,你可能想要像[WideCharToMultiByte](http://msdn.microsoft.com/en-us/library/dd374130 \(v = vs.85 \).aspx) – user786653

+0

感謝user786653,我嘗試了FindFirstFileA,但它有一個fspec的問題,所以我脫掉了演員LPCWSTR,然後就可以了。不幸的是,它有一個問題與「&」[errorC2664不能將參數2從WIN32_FIND_DATA *轉換爲LPWIN32_FIND_DATAA]我將尋找一個WideCharToMultiByte的例子,我認爲現在有點過頭了... – Acy

+0

@Acy:繼續下行,使用'WIN32_FIND_DATAA'而不是'WIN32_FIND_DATA'。 – dalle

回答

0

我相信你的問題是因爲的std :: string類型使用的基本字符的字符類型和運算符+不標準定義: :basic_string和寬字符數組。我認爲Visual Studio支持std :: wstring,如果放入而不是std :: string,可能會使其工作。

有幾種不同的字符串,因爲有很多不同的方式來編碼字符。 ASCII,UTF-8等等。不是所有的人都適合你的基本字符*,並且你在C++中並沒有真正與絕大多數程序隔離。

+0

我也試過wstring,但它是修復類似錯誤的無盡鏈。但我同意你和user786653,我將最終將整個事情轉換爲widechar類型。但是它有很多代碼,我沒有編寫任何代碼,我需要它來編譯,以便我可以學習它。我改變了很多類型以達到這一點,它似乎在不可兼容的類型之間來回切換,這在gcc中必須是可以的。無論如何,我真的希望我沒有把它弄得太糟糕,因爲它做了很多我需要知道如何去做的事情,它需要能夠在Visual Studio中編譯,因爲我不會是唯一使用它的人。 – Acy

3

std::string轉換pathstd::wstring

std::string s("hello"); 
std::wstring ws; 

ws.assign(s.begin(), s.end()); 

創建std::wstringcFileName,讓您同時擁有在同類型的字符串操作。

注意:該assign()方法僅適用,如果你要轉換從std::stringstd::wstring。反之對所有的Unicode字符都不能正常工作。發現這個codeguru.com論壇主題其中談到轉換std::string之間std::wstringHow to convert string to wstring?

參考:

MSDN:std::wstring

cplusplus.com:std::string/std::wstring

相關問題