需要连接std:string + WCHAR移动gcc代码到Visual c++ 2010
Need to concatenate std:string + WCHAR moving gcc code to Visual C++ 2010
下面的代码不是我写的,它是开源的,不再受支持。我确实需要对它做一些改变,所以我安装了vc++ Express 2010,我已经设法解决了大部分问题,这个应用程序实际上是为Windows编写的,所以虽然它一直很棘手,但相当困难,我正在取得一些进展。
我被困在一个转换,我还没有完全得到vc++ 2010类型转换的处理,但这里的代码给我我最后的头痛…
path为std::string, cFileName为WCHAR。我相信我成功地将path转换为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.0vcincludestring(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.0vcincludestring(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
将path
从std::string
转换为std::wstring
std::string s("hello");
std::wstring ws;
ws.assign(s.begin(), s.end());
为cFileName
创建一个std::wstring
,这样您就有相同类型的两个字符串来操作。
: assign()
方法仅在从std::string
转换为std::wstring
时有效。反之亦然,并不适用于所有Unicode字符。找到这个codeguru.com论坛主题,讨论std::string
和std::wstring
之间的转换:如何将字符串转换为wstring?
MSDN: std:: wstring
cplusplus.com std:: string/std:: wstring
我相信你的问题是因为std::string类型使用你的基本字符作为字符类型,+操作符没有为std::basic_string和宽字符数组定义。我认为Visual Studio支持std::wstring,如果将std::string替换为std::string,可能会使此工作顺利进行。
有几种不同的字符串因为有很多不同的方式来编码字符。ASCII, UTF-8,等等。并不是所有的都适合你的基本char*,在c++中,你并没有真正在很大程度上与之隔离。
- GCC对可能有效的代码抛出init list生存期警告
- GCC 和 Clang 代码性能的巨大差异
- GCC 能否优化具有相同主体的函数的代码大小?
- GCC 编译器是否应该对涉及 [[fallthrough]] 属性的格式错误的C++代码进行诊断?
- 如何使用 gcc 通过命令行限制C++代码的内存使用量?
- MSVC对这段代码很满意,但GCC并不那么热衷
- 为什么这段代码无法使用 gcc 4.8.5 编译,而使用 clang 编译正常
- 为什么以下代码在在线 ide(gcc 7.2.0) 上有效,但在 ubuntu 上出现错误?
- Boost tread 代码在 VC++ 下编译,但不在 GCC 下编译:如何获得更好的调试信息
- gcc 可以使用较旧的第三方库编译 C++17 代码吗?
- 如何使用 gcc 内联汇编器代码访问成员变量
- 初始化不可移动对象数组:为什么这样的代码无法在 GCC 上编译?
- 是否可以使用 GCC 编译具有特定编译器标志的代码文件的一部分?
- variadic模板代码中的GCC VS MSVC编译误差
- Clang vs GCC:挥发性访问的不同代码
- 为什么 GCC 6.3 在没有显式 C++11 支持的情况下编译此大括号初始化列表代码
- GCC 带内联汇编和 -Ofast 为内存操作数生成额外的代码
- 为什么以下代码不使用GCC编译,而是用Clang编译罚款
- 相同的代码在不同的 gcc 编译器中存在巨大的性能差异
- 非常量引用类型的无效初始化中出错.在Linux操作系统中使用c++代码.GCC编译器