使用 QString 在输出中获取奇怪的字符

发布于 2024-10-15 03:42:15 字数 548 浏览 3 评论 0原文

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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身软味 2024-10-22 03:42:15

尝试使用
输出 = 输出 + QString::fromStdString(*(infos.stops))+ "." ;

Try using
output = output + QString::fromStdString(*(infos.stops))+ "." ;

天煞孤星 2024-10-22 03:42:15

我想我在测试了你的应用程序后解决了这个问题。以下代码段应该可以做到这一点:

          output = output+ "Stops travelled: ";
          for(int a = 0; a < infos._numstops; ++a)
          {
              if(a)
                  output += ", ";
              output = output + QString::fromStdString(infos.stops[a]);
          }
          output = output + "<br>";

请注意,您有可用的成员infos._numstops并且应该使用它。如果您想输出逗号分隔的列表,则 if(a) 是一个不错的技巧。

(我运行了您的应用程序并注意到 info 结构不包括您开始路径的站点,而是包含路径结束的站点。您应该在输出中包含起始站点或进一步注意,if 正文中的 += 运算符是附加字符串的常用方法。)

I think i solved it after a bit testing your application. The following code segment should do it:

          output = output+ "Stops travelled: ";
          for(int a = 0; a < infos._numstops; ++a)
          {
              if(a)
                  output += ", ";
              output = output + QString::fromStdString(infos.stops[a]);
          }
          output = output + "<br>";

Note that you have the member infos._numstops availlable and should use it. The if(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.)

寻找一个思念的角度 2024-10-22 03:42:15

在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文