在“foo.cpp”中找到的左括号“(”未正确匹配

发布于 2024-10-08 10:46:32 字数 959 浏览 0 评论 0原文

错误的位置在下面的注释中指出。请帮助解决这个问题。

#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;
}

Location of the error is indicated in a comment below. Please help fix this.

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

笑看君怀她人 2024-10-15 10:46:32
if(strlen(str) + (strlen(ss.str) < sz)

应该

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

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

#include 进行更改的原始代码固定代码 请注意,我必须更改 string.h 中的 #include 为 ideone 的 cstring ,无论如何,我还必须添加 using namespace std; 因为您的代码没有明确使用 。需要时使用 std 命名空间,例如使用 cout 您还应该注意这些警告

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

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

should be

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

Notice how you have 4 ( but only 3 ) in the original line. These need to match up, but they don't. Since the ( before the second strlen() is superfluous (there is no need to wrap strlen(ss.str) in brackets), you can remove it.

Original code with changes to the #includes and fixed code. Note that I had to change the #includes from string.h to cstring for ideone. It's the preferred choice anyway. I also had to add using namespace std; as your code wasn't explicitly using the std namepsace where required, e.g. using cout. You should also take care of those warnings.

The reason the compiler error points to 4 lines down is because that's the line where it realises "Oh no, there's a problem here I'm never going to be able to parse this code!" GCC is pretty crappy at error reporting, but a but a compiler (e.g. clang) that does a better job of it would backtrack to the appropriate line where the root cause of the error actually lies and may even suggest a fix for you.

ㄖ落Θ余辉 2024-10-15 10:46:32

错误发生在这里:

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

可能应该是:

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

The error occurs here:

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

That probably should be:

if (strlen(str) + strlen(ss.str) < sz)
肥爪爪 2024-10-15 10:46:32

您在此行末尾缺少右括号:

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

You are missing a right parenthesis at the end of this line:

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