Reputation: 45
I have a try to binding data to textblock in vs 2013.
But I got some problem when I am try to convert list item to string.
this is the error message from vs :
no instance of constructor "std::basic_string<_Elem, _Traits,
_Alloc>::basic_string [with _Elem=wchar_t,
_Traits=std::char_traits<wchar_t>, _Alloc=std::allocator<wchar_t>]"
matches the argument list argument types are:
(std::_List_iterator<std::_List_val<std::_List_simple_types<std::string>>>)
this is my code :
list <string> c1;
//Insert Data
c1.push_back("one");
c1.push_back("two");
c1.push_back("three");
c1.push_back("Four");
c1.push_back("Five");
c1.push_back("Six");
c1.push_back("Seven");
c1.push_back("Eight");
c1.push_back("Nine");
c1.push_back("Ten");
//Random data from list
int RandNum = 0 + (std::rand() % 10);
auto en = c1.begin();
advance(c1.begin(), RandNum);
std::wstring s1(*en);
std::string s2(*en);
ENTEXT->Text = s2; //ENTEXT is textblock name
I am try to pass list element to textblock, but this code shows
error C2664 : 'void Windows::UI::Xaml::Controls::TextBlock::Text::set(Platform::String ^)' : cannot convert argument 1 from 'std::string' to 'Platform::String ^'
Upvotes: 3
Views: 10938
Reputation: 42888
en
is the iterator.*en
wstring
.Here's what it should be:
std::string s2(*en);
Btw the list elements are already strings, you don't need to convert anything.
To convert a std::string
to Platform::String
, you need to use the the c_str
member function:
ENTEXT->Text = en->c_str();
Upvotes: 3
Reputation: 43662
Since you wrote
"I got some problem when I am try to convert list item to string"
and not specifically wstring
, I suppose you meant to dereference the right iterator and have s2
as a normal std::string
copy
int RandNum = 0 + (std::rand() % 10);
auto en = c1.begin();
std::advance(en, RandNum); // Make sure to advance the right iterator
std::string s2(*en);
Notice that the above will create a copy of the list element the iterator points to, which, however, is already a std::string
(no conversion is needed). You could just use the dereferenced iterator:
std::cout << *en;
In case you really meant the string->wstring
conversion (for whatever reason not explained in the question), you could write:
int RandNum = 0 + (std::rand() % 10);
auto en = c1.begin();
std::advance(en, RandNum); // ditto
std::wstringstream ws;
ws << en->c_str();
std::wstring s2 = ws.str();
or, C++11 solution (mind the -stdlib=libc++
support):
//Random data from list
int RandNum = 0 + (std::rand() % 10);
auto en = c1.begin();
advance(en, RandNum);
typedef std::codecvt_utf8<wchar_t> convert_type;
std::wstring_convert<convert_type, wchar_t> converter;
std::wstring converted_str = converter.from_bytes(*en);
Upvotes: 1