在“foo.cpp”中找到的左括号“(”未正确匹配
错误的位置在下面的注释中指出。请帮助解决这个问题。
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该
注意原始行中有 4 个
(
但只有 3 个)
。这些需要匹配,但事实并非如此。由于第二个strlen()
之前的(
是多余的(无需将strlen(ss.str)
括在括号中),因此您可以删除它。对
#include
进行更改的原始代码 和 固定代码 请注意,我必须更改string.h 中的
为 ideone 的#include
。cstring
,无论如何,我还必须添加using namespace std;
因为您的代码没有明确使用。需要时使用 std
命名空间,例如使用cout
您还应该注意这些警告。编译器错误指向下面 4 行的原因是因为这是它意识到“哦不,有这里有一个问题,我永远无法解析这段代码!” GCC 在错误报告方面非常糟糕,但是一个编译器(例如 clang)做得更好,它会回溯到根所在的适当行错误的原因实际上存在,甚至可能会建议您修复。
should be
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 secondstrlen()
is superfluous (there is no need to wrapstrlen(ss.str)
in brackets), you can remove it.Original code with changes to the
#include
s and fixed code. Note that I had to change the#include
s fromstring.h
tocstring
for ideone. It's the preferred choice anyway. I also had to addusing namespace std;
as your code wasn't explicitly using thestd
namepsace where required, e.g. usingcout
. 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.
错误发生在这里:
可能应该是:
The error occurs here:
That probably should be:
您在此行末尾缺少右括号:
You are missing a right parenthesis at the end of this line: