我沒有寫下面的代碼,它是開源的,不再支持。我需要對它進行一些更改,所以我安裝了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
一個快速的解決方案是使用ansi版本的Find *'。這是明確使用'FindFirstFileA'。否則,你可能想要像[WideCharToMultiByte](http://msdn.microsoft.com/en-us/library/dd374130 \(v = vs.85 \).aspx) – user786653
感謝user786653,我嘗試了FindFirstFileA,但它有一個fspec的問題,所以我脫掉了演員LPCWSTR,然後就可以了。不幸的是,它有一個問題與「&」[errorC2664不能將參數2從WIN32_FIND_DATA *轉換爲LPWIN32_FIND_DATAA]我將尋找一個WideCharToMultiByte的例子,我認爲現在有點過頭了... – Acy
@Acy:繼續下行,使用'WIN32_FIND_DATAA'而不是'WIN32_FIND_DATA'。 – dalle