C++编译错误
我不明白为什么当我尝试编译时会出现这些错误。我从未遇到过“expected _ before _ token”错误,但我相信它们很常见(如果没有,请随时启发我)。
pe4.cpp:在函数
'int main()'
中:
pe4.cpp:18: 错误:在';'
令牌之前应有')'
pe4.cpp:18: 错误:在')'
令牌之前应有';'
pe4.cpp:45: 错误:在'{'
标记之前不允许使用函数定义
pe4.cpp:51: 错误:在'{'
标记之前不允许使用函数定义
pe4.cpp:57: 错误:在'{'
标记之前不允许使用函数定义
#include <iostream>
using namespace std;
void printStar(int);
void printSpace(int);
void printNewLine();
int main()
{
int side, i, j;
if (i=0; i < 2; i++)
{
cout << "Enter side: " << endl;
cin << side;
if (side < 3 || side > 20)
{
cout << "Out of Bounds!!!"
return 0;
}
printStar(side);
printNewLine();
{
printStar(1);
printSpace(side-2);
printStar(1);
printNewLine();
}
printStar(side);
printNewLine();
}
void printStar(int a)
{
for (int j = 0; j < a; j++)
cout << "*";
}
void printSpace(int a)
{
for (int j = 0; j < a; j++)
cout << " ";
}
void printNewLine()
{
cout << endl;
}
}
I can't figure out why I am getting these errors when I try to compile. I've never encountered the 'expected _ before _ token' error, but I believe they're common (if not feel free to enlighten me).
pe4.cpp: In function
'int main()'
:
pe4.cpp:18: error: expected')'
before';'
token
pe4.cpp:18: error: expected';'
before')'
token
pe4.cpp:45: error: a function-definition is not allowed here before'{'
token
pe4.cpp:51: error: a function-definition is not allowed here before'{'
token
pe4.cpp:57: error: a function-definition is not allowed here before'{'
token
#include <iostream>
using namespace std;
void printStar(int);
void printSpace(int);
void printNewLine();
int main()
{
int side, i, j;
if (i=0; i < 2; i++)
{
cout << "Enter side: " << endl;
cin << side;
if (side < 3 || side > 20)
{
cout << "Out of Bounds!!!"
return 0;
}
printStar(side);
printNewLine();
{
printStar(1);
printSpace(side-2);
printStar(1);
printNewLine();
}
printStar(side);
printNewLine();
}
void printStar(int a)
{
for (int j = 0; j < a; j++)
cout << "*";
}
void printSpace(int a)
{
for (int j = 0; j < a; j++)
cout << " ";
}
void printNewLine()
{
cout << endl;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
cout << 末尾没有
行。;
“出界!!!”你有
if (i=0; i < 2; i++)
;应该是for (i=0;...
。您有
cin << side
;应该是cin >> side
。您已经在
main()
内部定义了函数体;它们应该位于外部You have no
;
at the end of thecout << "Out of Bounds!!!"
line.You have
if (i=0; i < 2; i++)
; that should befor (i=0;...
.You have
cin << side
; that should becin >> side
.You have defined your function bodies inside
main()
; they should live outside.您正在 main() 定义中定义函数 printStar() 等。将这些函数移到 main() 的右括号之外。
You are defining your functions printStar(), etc, inside your main() definition. Move those functions outside of main()'s closing bracket.
int main()
方法的结束}
需要位于void printStart(int a)
之前。另外,您需要在
cout << 末尾添加
;
。 “越界!!!”The closing
}
of theint main()
method needs to go beforevoid printStart(int a)
.Also, you need a
;
at the end ofcout << "Out of Bound!!!"