我非常喜欢WxWidgets,并开始使用C++编程。我的示例程序将摄氏度从文本形式转换为华氏度。以下是我的基本代码:
//get "100" from textbox
wxString szCelsius = TextCtrl1->GetValue();
long lCelsius;
//attempt to cast into long
szCelsius.ToLong(&lCelsius, 10);
//formula that works in normal cases to get fahrenheit
long lFahrenheit = ((9.f/5.f) * lCelsius + 32);
//SOMEHOW this works:
//long lFahrenheit = ((9.f/5.f) * 100 + 32);
//display debug info, note it displays lCelsius as 100
wxString debuginfo;
debuginfo << _T("deg C: ") << lCelsius << _T("\n");
//displays incorrectly as 211
debuginfo << _T("deg F: ") << lFahrenheit << _T("\n");
//this displays 100
std::cout << lCelsius;
//this fails though
assert(lCelsius == 100);
现在有了调试信息,lCelcius像预期的一样是100,但它返回的华氏温度是211而不是212!奇怪的是,在纯C中,这个公式运行得很好,当我用100替换lCelsius时,它也能正常工作,尽管我的调试信息清楚地显示它是100。
你看到任何明显的问题吗?还是我只是不能做这么简单的事情?我不确定Wx在做什么,以使它比应该少一个。
编辑:包括assert.h并在调试器中运行lCelsius == 100会失败,但std::cout lCelsius返回100。肯定有什么问题出现在Wx中导致结果混乱,但仍然是“100”..
szCelsius.ToLong(&lCelsius, 10);
返回true
吗? - Vijay Mathew