开发者

Using QString get strange characters in the output

int a=0;
while (a<2)
{
    //infos.stops is point to one array, cal开发者_开发百科led 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:

Using QString get strange characters in the output

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.


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜