【问题标题】:Error passing strings and char to bool function将字符串和字符传递给布尔函数时出错
【发布时间】:2017-10-06 20:03:23
【问题描述】:

我正在尝试创建一个查看'-' 符号的函数,并根据它前面是否有' '(空格)来检查是减号还是负号。我这样做是通过比较我拥有的当前字符 (infix[x]) 并与infix[x+1] 进行比较;我不断收到错误,但我不确定是因为我没有正确通过还是其他原因?

        for(unsigned x = 0; x < infix.length(); ++x)
        {
             // place numbers (standard, decimal, & negative)
             // numbers onto the 'postfix' string
             if((isdigit(infix[x])) || (infix[x] == '.'))
             {
                 postfix += infix[x];
             }
             else if ((infix[x] == '-'))
             {
                     if(checkfornegative(infix[x], infix)== 1))  // error: expected primary-expression before ‘)’ token
            if(checkfornegative(infix[x], infix)== 1))
            {
                        postfix+= " ";
            }
            else if(checkfornegative(infix[x], infix)== 0))  //error: expected primary-expression before ‘)’ token
                if(checkfornegative(infix[x], infix)== 1))

                {
                    postfix += infix[x];
                }

            }

// This is the function in using
bool checkfornegative(char C, string& QQ)
{
         bool status;
         if((C == '-') && (QQ[C+1] == ' '))
         {
                 status = true;
         }
         else if((C == '-') && (QQ[C+1] != ' '))
         {
                 status =  false;
    }
    return status;
}

【问题讨论】:

  • 这里有一些关于一个最小的、完整的例子的上下文将有很大帮助。这些函数是如何调用的?您提供什么数据?
  • 你的括号不平衡,这不可能是你正在运行的代码。
  • QQ[C+1] 没有意义。 C 是一个字符,而不是字符串中的索引。

标签: c++ string function parsing


【解决方案1】:

您收到编译错误,因为您在 if 条件中有额外的右括号。

if(checkfornegative(infix[x], infix)== 1))  // error: expected primary-expression before ‘)’ token

从条件中删除最后一个右括号。第二个条件也一样。

但是,您的代码中有几个问题,但它们不是编译错误。

【讨论】:

  • 解决了,我这辈子都看不到了。非常感谢。
猜你喜欢
  • 2016-08-28
  • 2016-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 2013-12-11
  • 1970-01-01
相关资源
最近更新 更多