为什么我得到这个编译器错误试图定义一个函数体
Why do I get this compiler error trying to define a function body?
//Prints a box of X's with user unput of width and height. Uses a User-defined function
#include <iostream>
#include <cmath>
using namespace std;
void box(int height, int width, int h = 1, int w = 1);
int main() {
int width, height;
cout << "Please enter width (0-25): n";
cin >> width;
while (!(cin >> width) || width < 0 || width > 25) {
cout << "Invalid entry. Please re-enter width: n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
}
cout << "Please enter height (0-25): n";
cin >> height;
while (!(cin >> height) || height < 0 || height > 25) {
cout << "Invalid entry. Please re-enter height: n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
}
cin.ignore(numeric_limits<streamsize>::max(), 'n');
}
void box(int height, int width, int h, int w)
{
for (int h = 1; h <= height; h++)
{
for (int w = 1; w <= width; w++)
cout << "X";
cout << endl;
}
}
我已经做了建议的编辑,程序编译没有错误,但它不打印盒子。我知道我需要在main中定义函数,但是当我写:
box (width, height);
显示错误。另外,当我在命令提示符窗口时,输出如下所示:
请输入宽度(0-25):12
12请输入身高(0-25):12 <——为什么它在第一次拍摄时不接受这个数字?
z无效的条目。请重新输入高度:12
这一行
void box(int height, int width, int h, int w);
"declare"是一个名为box的函数,它接受四个参数,然后行尾的分号结束当前语句。
所以当你尝试定义 box时,你需要省略后面的分号
void box(int height, int width, int h, int w)
{
...
问题就在这里:
void box(int height, int width, int h, int w) // there shouldn't be a semicolon
{
int height, int width, int h, int w;
for (h = 1; h <= height; h++);
{
for (w = 1; w <= width; w++);
cout << "X";
cout << endl;
}
}
Catch!:)
#include <iostream>
#include <iomanip>
void box( size_t height, size_t width, char c = 'X' )
{
while ( height-- )
{
std::cout << std::setfill( c ) << std::setw( width ) << c << std::endl;
}
}
int main()
{
while ( true )
{
const size_t N = 25;
std::cout << "Please enter height and width "
"less than or equal to " << N << " (0-exit): ";
size_t width = 0, height = 0;
std::cin >> height >> width;
if ( height == 0 || width == 0 ) break;
if ( N < height ) height = N;
if ( N < width ) width = N;
std::cout << std::endl;
box( height, width );
std::cout << std::endl;
}
}
如果要输入例如
10 16
则程序输出将是
Please enter height and width less than or equal to 25 (0-exit): 10 16
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
Please enter height and width less than or equal to 25 (0-exit):
至于你的代码,那么函数定义中至少有一个错别字
void box(int height, int width, int h, int w);
^^
{
,没有必要在函数体
中重新声明形参void box(int height, int width, int h, int w);
{
int height, int width, int h, int w;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
同样在循环中你必须删除类型说明符和分号
for (int h = 1; h <= int height; h++);
^^^^ ^^^^ ^^
函数太复杂了。:)
编辑:对于你更新的代码,然后删除以下语句
cout << "Please enter width (0-25): n";
cin >> width; // <== remove
cout << "Please enter height (0-25): n";
cin >> height; // <== remove
和重写函数中的循环,如
for ( ; h <= height; h++)
{
for ( int w1 = w ; w1 <= width; w1++)
cout << "X";
cout << endl;
}
你还忘了在main中调用函数本身:)
相关文章:
- 在命名空间中定义函数还是限定函数
- 用C++在单独的头文件中完成函数体
- 为什么在定义函数之前先声明它
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- 我可以在 C++ 中的函数体之外进行操作吗?
- 这个c++代码是如何在没有定义函数的情况下运行的
- 具有外部"c"和程序集的未定义函数
- 已定义函数时出现 G++ "未定义的引用"错误
- 将自定义函数传递到基抽象类中以延迟执行
- C++使用 rand 定义函数语法
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题
- 将具有固定签名的自定义函数名称注入 CRTP
- 有没有办法在不使用 #ifdef 的情况下不编译发布版本中的单元测试函数体?
- 使用定义函数模板别名
- constexpr函数体c++14中可以有非文字类型的定义变量吗
- 在类定义之外的模板类成员函数体中,何时需要模板参数
- 如何在构造函数体中而不是在定义中初始化变量
- 为什么当我把构造函数定义放在体中而不是头文件中时,我得到了未定义的引用?
- 为什么我得到这个编译器错误试图定义一个函数体