You cannot assign a std::string
to a System::String
. The first one is ISO c++ and the second one Microsoft C++/CLI.
As suggested here, you can do something like this
String^ num = gcnew System::String(convert.str().c_str());
Question
I am trying to create a random number generator in C++, which puts the result in a textBox.
I get the error 'error C2440: 'initializing' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'System::String ^
My code is:
int rnd = 1 + rand() % 100;
std::ostringstream convert;
convert << rnd;
String ^ num = convert.str();
textBox1->Text = num;
What am I doing wrong?
Solution
You cannot assign a std::string
to a System::String
. The first one is ISO c++ and the second one Microsoft C++/CLI.
As suggested here, you can do something like this
String^ num = gcnew System::String(convert.str().c_str());
OTHER TIPS
The .Net way is
textBox1-> Text = rnd.ToString ();
ostringstream is a lousy way to do that conversion even in native C++.