我没有写下面的代码,它是开源的,不再支持。我需要对它进行一些更改,所以我安装了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