【问题标题】:Why do I get this compiler error trying to define a function body?为什么在尝试定义函数体时会出现此编译器错误?
【发布时间】:2015-06-07 10:31:54
【问题描述】:
//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中定义函数,但是当我写的时候:

框(宽、高);

它显示一个错误。另外,当我在命令提示符窗口中时,输出如下:

请输入宽度 (0-25): 12

12 请输入身高(0-25): 12

z 输入无效。请重新输入身高: 12 按任意键继续...

【问题讨论】:

  • 不应该有分号:...int w){... 而不是...int w);{..。此外,我想知道,为什么你有 2 个额外的函数声明(一个在最顶部,另一个在定义上方)。只需将函数(仅一次)放在 main 上方即可。
  • 在函数体开始之前有一个多余的分号。
  • 您的输入循环有问题。 cin.clear()cin.ignore 用于流失败时(例如,他们输入字母)。但是,您永远不会测试流是否失败,因此在这种情况下,您会通过读取未初始化的变量来导致 UB。循环应该是while ( !(cin &gt;&gt; width) || width &lt; 0 || width &gt; 25 ),或者预先将width初始化为一个超出范围的值。
  • for (int h = 1; h &lt;= int height; h++); 应该是 for (int h = 1; h &lt;= height; h++)w 循环的相同更改
  • 已编辑,但仍有问题

标签: c++ user-defined-functions


【解决方案1】:

这一行

void box(int height, int width, int h, int w);

“declare”是一个名为 box 的函数,它接受四个参数,然后行尾的分号结束当前语句。

因此,当您尝试定义框时,您需要省略尾随分号

void box(int height, int width, int h, int w)
{
   ...

【讨论】:

    【解决方案2】:

    问题出在这里:

    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;
        }
    }
    

    【讨论】:

    • 已编辑,但仍面临问题
    • 您面临什么问题?
    • 当我尝试在 main 中调用函数时,它说定义的参数数量有问题
    【解决方案3】:

    抓住!:)

    #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++ 代码很长时间了,我只是这么慢,因为我是新手 :)
    • @user4983123 我没有写过 C++ 程序,因为我失业了。:) 在上一份工作中,我为 IBM 大型机编写了 C 和 PL/X 程序。
    • 已编辑,但仍面临问题。我知道我的版本更令人困惑,但我不想完全更改代码。你能再看一眼吗?
    • @user4983123 还要注意函数的内循环是怎么写的。
    • 如何调用main中的函数?我写了 while (!(cin >> height) || height 25) { cout ::max(), '\n'); cin.ignore(numeric_limits::max(), '\n');盒子(高度,宽度); }
    猜你喜欢
    • 2013-08-14
    • 2020-06-30
    • 2023-03-30
    • 2015-07-27
    • 2011-01-28
    • 2013-03-07
    • 2021-12-28
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多