gpt4 book ai didi

c++ - 无法从 std::basic_string 转换为 int Visual Studio C++

转载 作者:太空宇宙 更新时间:2023-11-04 16:27:47 25 4
gpt4 key购买 nike

我写了一个小的数独程序,我想让它每次按下某个按钮时,该按钮上的文本是前一个数字加一。

例如,我有一个大按钮,上面写着“1”,我点击它,如果我再次点击它,它会显示“2”然后是“3”,依此类推直到“9”。

一开始我认为这会很简单,我使用这段代码调用了一个计数为 9 的整数,一个等于按钮文本的字符串,然后我尝试将 int 转换为字符串但我失败了,它给了我错误如下。这是代码:

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

这是错误:

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

我尝试在 MSDN 上搜索该错误,但它与我的不同,而且我离开页面时比输入时更加困惑。

另外作为引用,我在 Windows XP 和 Visual Studio 2010 C++ 中使用 Windows 窗体应用程序。

最佳答案

如果你想使用std::stringstreamstd::stringchar*转换为int >,它可能看起来像这样:

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

或者您可以使用 myStr 直接构造此 stringstream,这会产生相同的结果:

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

如果你想将 System::String^ 转换为 std::string,它可能看起来像这样:

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

虽然是Ben Voigt已指出:当您从 System::String^ 开始时,您应该改用 .NET Framework 中的某些函数来转换它。它也可能看起来像这样:

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

关于c++ - 无法从 std::basic_string 转换为 int Visual Studio C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10249859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com