I have an alarmingly strange situation. I fill my map with const char * and int values. When I try to "find" the one I want, it says it's not there. Here's the short version:

// tempText = "socialize"
for( actionIter =actionMap.begin(); actionIter !=actionMap.end(); ++actionIter )
{
	if ( !strcmp( actionIter->first, "socialize" ) )
	{
		print( "it reaches this point" );
		if ( !strcmp( actionIter->first, tempText ) ) // check if they're the same
			print( "it reaches this point too, they're the same" );
		iter =actionMap.find( tempText ) );
		if ( iter == actionMap.end() )
			print( "always hits this" );
		// so there's no "socialize" in the map (there is!)


		iter = actionMap.find( actionIter->first );
		if ( iter == actionMap.end() )
			print( "never hits this" );
		else
			print( "always hits this" );
		// but I already proved that tempText == actionIter->first!
		//how can this be found and tempText not?


		iter = actionMap.find( "socialize" );
		if ( iter == actionMap.end() )
			print( "always reaches this" );
		// claiming it doesn't find the very thing that let it into the if statement
	}

}

There's no threading or garbage collection going on. I thought it might have something to do with my changing of wchar to const char * when populating the map:

// this is inefficient but I did it for clarity during bug finding
const char* strcpy_wcr( const WCHAR *src )
{
	char* temp;
	const WCHAR* ptrSrc = src;
	int i = 0;

	while ( *ptrSrc )
		i++;

	char *temp = (char *)malloc( i + 1 ); // 1 for \0
	char *ptrTemp = temp;

	while (*src)
		*ptrTemp++ = *src++;

	*ptrTemp = 0;
	return static_cast<const char *>( temp );
}

//and the map is populated with this line
actionMap.insert( std::pair<const char*, int>( strcpy_wcr( pwszValue ), atoi( strText ) ) );
//where pwszValue  is a const WCHAR *

Any ideas?

I simplied the problem example but I still ahve no solution:

actionMap.insert( std::pair<const char*, int>( strcpy_wcr( pwszValue ), atoi( strText ) ) );
std::map<const char *, int>::iterator iter;
iter = actionMap.find( strcpy_wcr( pwszValue ) ); // doesn't find anything

//and the conversion function
const char* strcpy_wcr( const WCHAR *src )
{
	int offset = 0;

	while ( *( src + offset ) )
		Msg( "%c", *( src + offset++ ) );

	char *temp = (char *)malloc( offset + 1 ); // 1 for \0

	offset = 0;
	while (*( src + offset ) )
		*( temp + offset ) = *( src + offset++ );
	
	*( temp + offset ) = 0;
	return static_cast<const char *>( temp );
}

Please help :( I just can't figure this out.

Okay, I fixed it. I just changed all const char * types to std::string. It's a hack and I don't know why it worked but for people Googling this years from now, that's a workaround.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.