2011-07-12 49 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

相关问题