繁体   English   中英

C++ 中的结构,初始化

[英]Structures in C++, initializations

class box{
    int l,w,h;
    box(int a,int b,int c){
        l = a;w = b;h = c;
    };
    
};
bool cmp(box a,box b){
    return a.l*a.w<b.l*b.w;
}
class Solution{
    public:
    
    /*The function takes an array of heights, width and 
    length as its 3 arguments where each index i value 
    determines the height, width, length of the ith box. 
    Here n is the total no of boxes.*/
    int maxHeight(int h[],int w[],int l[],int n)
    {
        //Your code here
        struct box b[(3*n)]={};
        int j = 0;
        for(int i = 0 ; i< n; ++ i){
            b[j++] = box(min(l[i],w[i]),max(l[i],w[i]),h[i]);
            b[j++] = box(min(l[i],h[i]),max(l[i],h[i]),w[i]);
            b[j++] = box(min(w[i],h[i]),max(w[i],h[i]),l[i]);
        }
        n*=3;
        sort(b,b+n,cmp);
        int dp[n];
        for(int i = 0 ; i< n; ++ i){
            dp[i] = b[i].h;
        }
        int ans = INT_MIN;
        for(int i = 1; i< n; ++ i){
            for(int j = 0; j< i; ++ j){
                if(b[i].l>b[j].l and b[i].w>b[j].w and dp[i]<dp[j]+b[i].h)
                dp[i] = b[i].h+dp[j];
            }
            ans = max(ans,dp[i]);
        }
        return ans;
    }
};

上面的代码显示了一个错误,即结构变量采用 3 arguments 并且仅给出 1,如图所示。 请帮我解决这个问题。 谢谢。

还请提供一些很好的参考资料,从中我可以对这些主题有更多的了解。 就像我可以从中学习这些类型的提示和语言秘密的网站。 在此处输入图像描述

我知道了。 在这里我没有编写默认的初始化函数,这就是它显示此错误的原因。 我还需要将访问修饰符更改为 public。 所以我将结构声明的代码更改为这样的代码。

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM