qq_34324657 2016-04-10 03:49 采纳率: 25%
浏览 1327
已采纳

烦请各位大神给小妹瞅瞅

#include
using namespace std;
class xx
{
public:
xx()
{
quantity=0;
price=0;
}
void total();
static float average();
static void display();
private:

int quantity;
float price;
static double discount;
static float sum;
static int n;

};
void xx:: total()
{

if(quantity>=10)
    price=0.98*price;
    sum=quantity*price;
n+=quantity;

}float xx::average()
{
return(sum/n);
}
double xx::discount=0.98;
float xx::sum=0;
int xx::n=0;
void xx::display()
{
cout<<"sum:"<<sum<<" "<<"average:"<<xx::average()<<endl;
}
int main()
{
xx t1[3]={xx(5,23.5),
xx(12,24.56),
xx(100,21.5),
}
t1[0].total ();

t1[1].total ();

t1[2].total ();

t1.display();
return 0;

}
问题;C:\Users\dell\Desktop\8.cpp(42) : error C2661: 'xx::xx' : no overloaded function takes 2 parameters

  • 写回答

1条回答 默认 最新

  • 小灸舞 2016-04-10 04:15
    关注

    因为楼主下面xx(5,23.5)是这样创建对象的,但又没有重载的构造函数满足,所以会报错
    在类xx里面定义一个这样的构造函数就行了
    另外还有两个小地方要改一下,我都标注了

     #include<iostream>
    #include<string>
    using namespace std;
    class xx
    {
    public:
        xx()
        {
            quantity=0;
            price=0;
        }
        xx(int a, float b) //第一处问题
        {
            quantity = a;
            price = b;
        }
        void total();
        static float average();
        static void display();
    private:
        int quantity;
        float price;
        static double discount;
        static float sum;
        static int n;
    };
    void xx:: total()
    {
        if(quantity>=10)
            price=0.98*price;
        sum=quantity*price;
        n+=quantity;
    }float xx::average()
    {
        return(sum/n);
    }
    double xx::discount=0.98;
    float xx::sum=0;
    int xx::n=0;
    void xx::display()
    {
        cout<<"sum:"<<sum<<" "<<"average:"<<xx::average()<<endl;
    }
    int main()
    {
        xx t1[3]={xx(5,23.5),
            xx(12,24.56),
            xx(100,21.5),
        };             //第二处,少了分号
        t1[0].total ();
        t1[1].total ();
    
        t1[2].total ();
    
        t1[0].display(); //第三处
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮