Find method in std::wstring - c++

I have declared Wstring as follows
wstring strID
When I try to find the occurrences sub-string as follows
int index = strID.find("LABS");
I am getting error like the following
error C2664: 'unsigned int std::basic_string<_Elem,_Traits,_Ax>::find(const std::basic_string<_Elem,_Traits,_Ax> &,unsigned int) const' : cannot convert parameter 1 from 'const char [13]' to 'const std::basic_string<_Elem,_Traits,_Ax> &'
Can you please help me to find the occurrences of sub-string?

When searching a wstring, you need to have the parameter as a wide string as well
int index = strID.find(L"LABS");
^

int index = strID.find(L"LABS");
EDIT:
http://msdn.microsoft.com/en-us/library/69ze775t(v=vs.80).aspx

Related

'HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from 'const char *' to 'LPCWSTR'

I'm still rather new to C++ but I've ran into a problem I can't solve, this is my error message:
'HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from 'const char *' to 'LPCWSTR'
And this the line that is throwing the error:
ModuleHandle = (DWORD)GetModuleHandle(moduleName.c_str());
You are passing a char * to something that needs a wchar_t *. You'll have to either convert your stringtype to wchar_t *, for example using the MultiByteToWideChar function (https://msdn.microsoft.com/en-us/library/windows/desktop/dd319072%28v=vs.85%29.aspx), or you can use the non-wide version of GetModuleHandle by calling GetModuleHandleA() instead of GetModuleHandleW().

Casting error converting from unsigned int to uint16_t

Why do I get this error
1>c:\users\g\documents\visual studio
2012\projects\tlvdemo\tlvdemo\tlvobject.cpp(50): error C2440: 'type
cast' : cannot convert from 'unsigned int (__thiscall
std::basic_string<_Elem,_Traits,_Alloc>::* )(void) throw() const' to
'uint16_t'
and how to fix it?
Code
uint16_t tagLen = (uint16_t)m_tagName.length; //m_tagName is string type
The problem is with the way you are calling the length method.
You have to use m_tagName.length();

C++03: _ui64toa_s: error C2446: ':' : no conversion from 'errno_t' to 'const char *'

I have the following line of code:
(total_time== 0xAAAABBBB) ? ("") : _ui64toa_s(total_time, myArray, 1024, 10);
As the title suggests, I receive this error:
error C2446: ':' : no conversion from 'errno_t' to 'const char *'
I am unsure how to convert errno_t to const char *.
I tried to solve it by writing
const char *n = _ui64toa_s(total_time, myArray, 1024, 10);
only to see this:
error C2440: 'initializing' : cannot convert from 'errno_t' to 'const char *'
_ui64toa_s does not return a string like _ui64toa does.
Instead it returns an errno_t which is defined as int.
errno_t e = _ui64toa_s(total_time, myArray, 1024, 10);
But since you're using C++ I recommend you use std::to_string instead.
std::to_string(total_time).c_str();
You might also want to take a look at this question.

Initializing auto variables in VC++ 2012

I am new to VC++ 2012. I have this code snippet.
auto it = query_map.find(U("callback"));
The problem is right under the dot there is a red line the error is
Error 1 error C2664: 'std::_Tree_iterator std::_Tree::find(const http::uri::encoded_string &)' : cannot convert parameter 1 from 'const wchar_t [9]' to 'const http::uri::encoded_string &' d:\maverick\projects\strikeforce\src\server\server\server.cpp 26
Can someone tell me a solution to this error?
Error 1 error C2664: 'std::_Tree_iterator std::_Tree::find(const http::uri::encoded_string &)'
This is a problem with the method find() from the class std::Tree
cannot convert parameter 1 from 'const wchar_t [9]' to 'const http::uri::encoded_string &'
The find() method must be used with a paremeter of type 'const http::uri::encoded_string &', but you passed a 'const wchar_t [9]'.
the method U() you have used returns an array of chars, but the find() method need another type of object.

How can I use SimpleIni to return wchar_t and then convert to std::wstring?

SimpleIni Documentation says wchar_t is supported but I don't understand how to use it. This is what I tried:
CSimpleIniCaseW ini;
ini.LoadFile("myapp.ini");
std::wstring test(ini.GetValue("testsection", "testkey", ""));
error C2664:
'CSimpleIniTempl::GetValue'
: cannot convert parameter 1 from
'const char [12]' to 'const wchar_t *'
You need to specify your strings as L"testsection" << Note the L.