使用 QString 在输出中获取奇怪的字符
int a=0;
while (a<2)
{
//infos.stops is point to one array, called abc[10]
output = output + QString::fromStdString(*infos.stops)+ "." ;
infos.stops++;
a++;
}
ui->showMsg->setText(output);
问题是 infos.stops 确实显示了,但出现了一些有趣的字符,例如:
我已上传所有源代码在QT Designer中设计的代码 http://uploading.com/files/eaddfaf8/bus.zip/ 问题行位于 manager.cpp 第 133 行。
int a=0;
while (a<2)
{
//infos.stops is point to one array, called abc[10]
output = output + QString::fromStdString(*infos.stops)+ "." ;
infos.stops++;
a++;
}
ui->showMsg->setText(output);
The problem is infos.stops did show, but some funny characters appear like:
I have uploaded all my source code which is designed in QT Designer
http://uploading.com/files/eaddfaf8/bus.zip/
The problem line is at manager.cpp line 133.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
输出 = 输出 + QString::fromStdString(*(infos.stops))+ "." ;
Try using
output = output + QString::fromStdString(*(infos.stops))+ "." ;
我想我在测试了你的应用程序后解决了这个问题。以下代码段应该可以做到这一点:
请注意,您有可用的成员
infos._numstops
并且应该使用它。如果您想输出逗号分隔的列表,则if(a)
是一个不错的技巧。(我运行了您的应用程序并注意到
info
结构不包括您开始路径的站点,而是包含路径结束的站点。您应该在输出中包含起始站点或进一步注意,if 正文中的+=
运算符是附加字符串的常用方法。)I think i solved it after a bit testing your application. The following code segment should do it:
Note that you have the member
infos._numstops
availlable and should use it. Theif(a)
is a nice trick if you want to output a comma separated list.(I ran your application and noticed that the
info
struct does not include the stop where you're starting your path but the one where it ends. You should include the starting stop in the output or exclude the target stop. Further note that the+=
operator like in the if-body is a common way to append strings.)在 manager.cpp:103 中,您正在调用 DE1.cost(X,Y)。此方法在堆栈 (datzz.cpp:432) 上创建一个 std::string 数组(已传递),并在 datzz.cpp:502 处执行
c.stops = pass;
,它存储一个指向c. 在 DatzEzy::info 实例的 stop 变量中分配在堆栈上的内存块。当方法 cost(string, string) 返回时,为 pass 分配的内存将被释放,并且您的输出将是垃圾。切勿存储指向堆栈分配对象的指针。
顺便说一句,在函数调用中传递(只读)字符串时,您应该考虑使用 const 引用,这样可以避免昂贵的复制。
In manager.cpp:103 you are calling DE1.cost(X,Y). This method creates a std::string array (passed) on the stack (datzz.cpp:432) and at datzz.cpp:502 you do
c.stops = passed;
which stores a pointer to a memory block allocated on the stack in the stops variable of your DatzEzy::info instance c. When the method cost(string, string) returns, the memory allocated for passed is freed and your output will be garbage. Never store pointers to stack allocated objects.
By the way, you should consider using const references when passing (read-only) strings in function calls, which avoids expensive copying.