Left bracket '(' found at 'foo.cpp' was not matched correctly

The location of the error is indicated in the 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;
}
+3
source share
3 answers

The error is here:

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

This should probably be:

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

it should be

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

Note that you have 4 (, but only 3 )in the original line. They must match, but they do not. Since (the second one is strlen()superfluous (there is no need to wrap it in brackets strlen(ss.str)), you can delete it.

#include s . , #include string.h cstring ideone. . using namespace std;, std namepace, , . cout. .

, 4 , - , , " , , !" GCC , (, clang), , , .

+8

The right bracket is missing at the end of this line:

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

Source: https://habr.com/ru/post/1781225/


All Articles