【问题标题】:Qt5.5 QString indexOf odd resultQt5.5 QString indexOf奇数结果
【发布时间】:2016-04-07 12:52:17
【问题描述】:

我正在使用 Qt Creator 开发 C++ 应用程序和调试器来检查代码,我试图了解调试器报告的一些非常奇怪的结果。

    if ( intDelimiter == -1
      && (intOpB = strProcessed.indexOf("[")) >= 0
      && (intClB = strProcessed.indexOf("]", ++intOpB) >= 0) ) {
        strRef = strProcessed.mid(intOpB, intClB - intOpB);

        if ( pobjNode != NULL ) {
            strProcessed.replace(strRef, pobjNode->strGetAttr(strRef));
        }

我在行上有一个断点:

    strRef = strProcessed.mid(intOpB, intClB - intOpB);

在上面的代码sn-p中strProcessed包含:

    "1079-[height]"

当断点被命中时,intClB 包含 1,intOpB 包含 6。

intOpB 是正确的,因为 indexOf 的返回值是 5,然后在搜索“]”之前递增,但是 intClB 不正确,为什么调试器将其报告为 1?这对我来说毫无意义。

我正在使用:

    Qt Creator 3.6.0
    Based on Qt 5.5.1 (GCC 4.9.1 20140922 (Red Hat 4.9.1-10), 64bit)
    Built On Dec 15 2015 01:01:12
    Revision: b52c2f91f5

正如 king_nak 所发现的,更正后的代码应为:

    if ( intDelimiter == -1
    && ((intOpB = strProcessed.indexOf("[")) >= 0
    && (intClB = strProcessed.indexOf("]", ++intOpB)) >= 0) ) {
        strRef = strProcessed.mid(intOpB, intClB - intOpB);

        if ( pobjNode != NULL ) {
            strProcessed.replace(strRef, pobjNode->strGetAttr(strRef));
        }
    }

【问题讨论】:

  • 这些值是否仅在调试器中?打印值时会发生什么?
  • 是的,这些是调试器报告的值,我现在试试打印。

标签: c++ qt-creator qt5.5


【解决方案1】:

你放错了大括号:

(intClB = strProcessed.indexOf("]", ++intOpB) >= 0)

这会将strProcessed.indexOf("]", ++intOpB) >= 0 的结果分配给intClB,解释为int。因为这个语句是trueintClB = 1

你想要:

(intClB = strProcessed.indexOf("]", ++intOpB) ) >= 0
                                              ^ Brace here

【讨论】:

    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多