简体   繁体   中英

Need to concatenate std:string + WCHAR moving gcc code to Visual C++ 2010

I did not write the code below, it's open source and no longer supported. I do need to make a few changes to it so I installed VC++ Express 2010 and I've managed to work out most of the problems, and the application was actually written for Windows to begin with so while it's been hair-pulling but reasonably difficult, I am making some progress.

I am stuck on one conversion and I haven't quite gotten a handle on VC++ 2010 type conversions, but here is the code giving me my last headache...

path is std::string and cFileName is WCHAR. I beleive I was successful in converting path to WCHAR, but then mEntries.push_back had a problem with it. What I beleive I need to do is convert cFileName to a string, and I have searched and tried numerous different ways to do it but I'm either getting the syntax wrong or it's not working in VC++ or I'm completely missing something else.

It would be nice to know why it does not work and why there are soooo many different "types" of strings (I write SQL Scripts, this is ridiculous) but at this point, I just need help making it work.

// 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;
}

The Error message is:

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

Convert path from std::string to std::wstring .

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

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

Create a std::wstring for cFileName , so that you have both the strings in the same type to operate.

NOTE : the assign() method works only if you are going to convert from std::string to std::wstring . The vice-versa doesn't work properly for all Unicode characters. Found this codeguru.com forum topic which talks about converting between std::string and std::wstring : How to convert string to wstring?

Reference:

MSDN: std::wstring

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

I believe your problem is because the std::string type uses your basic char as the character type and the + operator isn't defined for std::basic_string and a wide character array. I think Visual Studio supports std::wstring which might just make this work if dropped in instead of std::string.

And there are several different kinds of string because there are a lot of different ways to encode characters. ASCII, UTF-8, etc., etc. Not all of them fit in your basic char* and you're not really insulated from that to any great degree in C++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM