简体   繁体   中英

cannot convert from std::basic_string to int Visual studio C++

I've written a small sudoku program and I want to make it so each time you press a certain button the text on that button is the previous number incremented by one.

So, for instance, I have a large button that says "1" and I click on it, it will say "2" then "3" if I click it again, and so on until "9".

At first I thought it would be pretty simple, I used this code to call an integer that counts to 9, a string that equals the button text and then I tried to convert int to string and I failed, it gave me the error bellow. This is the code:

int s = 0;
String^ mystr = a0->Text;
std::stringstream out;
out << s;
s = out.str(); //this is the error apparently.
s++;

And this is the error:

error C2440: '=' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'int'

I tried searching on MSDN for that error but it's different than mine, and I left the page more confused than when I entered it.

Also for reference, I'm using a Windows Forms Application, in Windows XP, in Visual Studio 2010 C++.

If you want to convert std::string or char* to int by using std::stringstream , it could look like this:

int s = 0;
std::string myStr("7");
std::stringstream out;
out << myStr;
out >> s;

or you can construct this stringstream directly by using myStr which yields same result:

std::stringstream out(myStr);
out >> s;

And if you want to convert System::String^ to std::string , it could look like this:

#include <msclr\marshal_cppstd.h>
...
System::String^ clrString = "7";
std::string myStr = msclr::interop::marshal_as<std::string>(clrString);

although as Ben Voigt has pointed out: when you start with System::String^ , you should convert it by using some function from .NET Framework instead. It could also look like this:

System::String^ clrString = "7";
int i = System::Int32::Parse(clrString);

Since you're starting with String^ , you want something like:

int i;
if (System::Int32::TryParse(a0->Text, i)) {
    ++i;
    a0->Text = i.ToString();
}

s is of type int . str() returns a string . You can't assign a string to an int. Use a different variable to store the string.

Here's some possible code (albeit it won't compile)

string text = GetButtonText(); //get button text
stringstream ss (text); //create stringstream based on that
int s; 
ss >> s; //format string as int and store into s
++s; //increment
ss << s; //store back into stringstream
text = ss.str(); //get string of that
SetButtonText (text); //set button text to the string

There are a lot of ways to convert a string to an int in C++ --the modern idiom may be to install the boost libraries and use boost::lexical_cast.

However, your question indicates you don't have a very good grasp of C++. If the point of your effort is to learn more about C++, you might want to try one of the many simpler tutorials before you try something as complicated as a sudoku.

If you just want to build a sudoku with Windows forms, I'd recommend you drop C++ and look at C# or VB.Net, which have many fewer pitfalls for an inexperienced programmer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM