Problem with different types, WCHAR

I am trying to fill vector src.Files with values of file names, having problems with different types. When I try to pass search_data.cFileName directly to the vector it showed error:


When I try to pass the
cannot convert parameter 1 from 'WCHAR [260]' to 'const std::basic_string<_Elem,_Traits,_Ax> &'

When I try to separate it to variable test. It shows error
C2440: '=' : cannot convert from 'WCHAR [260]' to 'WCHAR *[260]'
There is no context in which this conversion is possible

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int FILE_::findFiles(std::string path, SRC& src )
{
	WIN32_FIND_DATA search_data;
	memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
	HANDLE handle = FindFirstFile(L"c:\\*", &search_data);
	WCHAR * test[260];

	while(handle != INVALID_HANDLE_VALUE)
	{
		std::wcout<<"\n"<<search_data.cFileName;
		test = search_data.cFileName;
		src.Files.insert(src.Files.begin(), test);
		if(FindNextFile(handle, &search_data) == FALSE)
		break;
	}
	FindClose(handle);
	return 0;
}
You're environment has _UNICODE defined. That switches on the native Uncode-16 strings in the Windows SDK.

You have a choice to make:
1. You can change your program to use std::wstring instead.

2. You can undefine _UNICODE to make the system provide you with char strings.

3. You can call the ASCII versions of those functions explicitly, to bypass the _UNCODE treatment as in:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int FILE_::findFiles(std::string path, SRC& src )
{
	WIN32_FIND_DATAA search_data;
	memset(&search_data, 0, sizeof(WIN32_FIND_DATAA));
	HANDLE handle = FindFirstFileA("c:\\*", &search_data);
//	WCHAR * test[260];

	while (handle != INVALID_HANDLE_VALUE)
	{
		std::cout << "\n" << search_data.cFileName;
//		test = search_data.cFileName;
//		src.Files.insert(src.Files.begin(), test);
		if (FindNextFileA(handle, &search_data) == FALSE)
			break;
	}
	FindClose(handle);
	return 0;
}
C2440: '=' : cannot convert from 'WCHAR [260]' to 'WCHAR *[260]'
1
2
3
WCHAR foo[260];
WCHAR  *  bar[260];
bar = foo; // error C2440 

Can you not see why?

Be glad that you made a mistake; for there is no copy assignment for plain arrays.

The original error, "cannot convert WCHAR [260] to std::string", is clear too:
vector::insert() expects a reference to std::string, but you tried to offer an array of WCHAR.

How would you create a string from array of characters?
std::string and UNICODE (WCHAR) are not compatible.

You may change your project setting/project defaults regarding the character set:

http://msdn.microsoft.com/en-us/library/8x480de8.aspx

to multibytes (not UNICODE)
I thought that UNICODE is multibyte... That was why I used it. I changed settings in the project to use Multibyte. Is there now some macro which identifies that the multibyte is defined? So I used the code of kbw - thanks, it works now :-).
Last edited on
Topic archived. No new replies allowed.