【问题标题】:The left parenthesis '(' found at 'foo.cpp' was not matched correctly在 'foo.cpp' 中找到的左括号 '(' 未正确匹配
【发布时间】:2011-05-28 06:05:40
【问题描述】:

错误的位置在下面的评论中指出。请帮助解决此问题。

#include<iostream.h>
#include<string.h>
#include<stdlib.h>
class String
{
private:
    enum{sz=80};
    char str[sz];
public:
    String()
    {
        strcpy(str,"");
    }

    String (char s[])
    {
        strcpy(str,s);
    }

    void display() const
    {
        cout<<str;
    }
    String operator +(String ss) const
    {
        String temp;
        if(strlen(str) + (strlen(ss.str) < sz)
        {
            strcpy(temp.str,str);
            strcat(temp.str , ss.str);
        } // Error is reported here!
        else
        {
            cout<<"\nstring overflow";
            exit(1);
        }
        return temp;
    }

};
int main()
{
    String s1="\nMerry christmas";
    String s2="happy new year";
    String s3;

    s1.display();
    s2.display();
    s3.display();

    s3=s1+s2;
    s3.display();
    cout<<endl;
    return 0;
}

【问题讨论】:

  • 你用什么来编辑你的源代码?一个好的编辑器通常会在视觉上为您指明正确的方向。
  • 你知道标准库string类,对吧?

标签: c++ compiler-errors


【解决方案1】:

您在这一行的末尾缺少一个右括号:

if(strlen(str) + (strlen(ss.str) < sz)

【讨论】:

    【解决方案2】:

    错误发生在这里:

    if(strlen(str) + (strlen(ss.str) < sz)
    

    应该是这样的:

    if (strlen(str) + strlen(ss.str) < sz)
    

    【讨论】:

      【解决方案3】:
      if(strlen(str) + (strlen(ss.str) < sz)
      

      应该是

      if(strlen(str) + strlen(ss.str) < sz)
      

      注意原始行中有 4 个 ( 但只有 3 个 )。这些需要匹配,但事实并非如此。由于第二个strlen() 之前的( 是多余的(无需将strlen(ss.str) 括在括号中),您可以将其删除。

      Original code with changes to the #includesfixed code。请注意,对于 ideone,我必须将 #includes 从 string.h 更改为 cstring。无论如何,它是首选。我还必须添加using namespace std;,因为您的代码没有在需要的地方明确使用std namepsace,例如使用cout。你也应该take care of those warnings

      编译器错误指向以下 4 行的原因是因为它在该行中意识到“哦,不,这里有一个问题,我永远无法解析这段代码!” GCC 在错误报告方面相当糟糕,但是一个做得更好的编译器(例如 clang)会回溯到错误的根本原因所在的适当行,甚至可能会为您提供修复建议。

      【讨论】:

        猜你喜欢
        • 2012-02-25
        • 2014-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多