为什么我得到这个编译器错误试图定义一个函数体

Why do I get this compiler error trying to define a function body?

本文关键字:定义 函数体 一个 错误 为什么 编译器      更新时间:2023-10-16
//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中调用函数本身:)