为什么我会在B中获得此错误请求,该请求是非类型框中的B中的BENT [5]

发布于 2025-01-26 17:22:12 字数 1488 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃繁缕 2025-02-02 17:22:12

强调文本在main中声明的变量B

box b[5];

具有数组类型。数组没有成员功能。因此,此陈述

b.set(5);

是不正确的。例如,您可以写作,

b[0].set( 5 );

但是在成员函数集中,使用了调用

        b[i].length=len;
        b[i].breadth=bre;
        b[i].height=hei;
        b[i].volume=vol;

未定义行为的非直接指针b

因此,您的代码只是没有意义。

似乎您需要一个静态成员函数,例如

static void set(box b[], int n)
{
    for(int i=0;i<n;i++)
    {
        int len,bre,hei,vol;
        cout<<"enter length"<<endl;
        cin>>len;
        cout<<"enter breadth"<<endl;
        cin>>bre;
        cout<<"enter height"<<endl;
        cin>>hei;
        cout<<"enter volume"<<endl;
        cin>>vol;
        b[i].length=len;
        b[i].breadth=bre;
        b[i].height=hei;
        b[i].volume=vol;
    }
}

box b[5];
box::set( b, 5 );

删除数据成员B和N。

int n;
box *b;

在这种情况下,您应该在类定义中

可以按照以下方式声明和定义函数获取

void get() const
{
    cout<<"length "<< length << '\n';
    cout<<"breadth "<< breadth << '\n';
    cout<<"height "<< height << '\n';
    cout<<"volume "<< volume << '\n';
}

,对于主要的声明数组,函数可以称为以下方式

for ( const auto &item : b )
{
    item.get();
    cout << '\n';
}

emphasized textThe variable b declared in main

box b[5];

has an array type. Arrays do not have member functions. So this statement

b.set(5);

is incorrect. You could write for example

b[0].set( 5 );

However within the member function set there is used uninitialized pointer b

        b[i].length=len;
        b[i].breadth=bre;
        b[i].height=hei;
        b[i].volume=vol;

that invokes undefined behavior.

So your code as is just does not make a sense.

It seems you need a static member function like for example

static void set(box b[], int n)
{
    for(int i=0;i<n;i++)
    {
        int len,bre,hei,vol;
        cout<<"enter length"<<endl;
        cin>>len;
        cout<<"enter breadth"<<endl;
        cin>>bre;
        cout<<"enter height"<<endl;
        cin>>hei;
        cout<<"enter volume"<<endl;
        cin>>vol;
        b[i].length=len;
        b[i].breadth=bre;
        b[i].height=hei;
        b[i].volume=vol;
    }
}

And call it like

box b[5];
box::set( b, 5 );

In this case you should remove the data members b and n

int n;
box *b;

in the class definition.

The function get can be declared and defined the following way

void get() const
{
    cout<<"length "<< length << '\n';
    cout<<"breadth "<< breadth << '\n';
    cout<<"height "<< height << '\n';
    cout<<"volume "<< volume << '\n';
}

And for the declared array in main the function can be called the following way

for ( const auto &item : b )
{
    item.get();
    cout << '\n';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文