注册 登录
编程论坛 C++教室

编程题,可以帮我看下错在那里吗?

jinge1001 发布于 2016-12-10 23:48, 1526 次点击
程序代码:
#include "stdafx.h"
#include <iostream>
using namespace std;

class Yuebao
{
    static double profitRate;//声明利息
    double yue;//余额

public:
    static void setProfitRate(double rate);
    /* Your code here! */
    Yuebao(double a):yue(a){}//构造函数
    void addProfit() {   
         yue+=yue*profitRate;
    }
    void deposit(double amountin) {
        yue += amountin;
    }
    void withdraw(double amountout) {
        yue-=amountout;
    }
    double getBalance() {
        return yue;
    }
};

void Yuebao::setProfitRate(double rate) { //定义余额
        profitRate = rate;
}
int main()
{
    int n;
    while (cin >> n)
    {
        double profitRate;
        cin >> profitRate;
        Yuebao::setProfitRate(profitRate);//设定鱼额宝的利率
        Yuebao y(0); //新建鱼额宝账户,余额初始化为0
        int operation;//接受输入判断是存还是取
        double amount;//接受输入存取金额
        for (int i = 0; i < n; ++i)
        {
            y.addProfit();//加入前一天余额产生的利息
            cin >> operation >> amount;
            if (operation == 0)
                y.deposit(amount);//存入金额
            else
                y.withdraw(amount);//取出金额
        }
        cout << y.getBalance() << endl;//输出最终账户余额
    }
    return 0;
}


生成后显示下面的错误,试了很久也没弄清楚怎么回事。
只有本站会员才能查看附件,请 登录

4 回复
#2
yangwawa2016-12-11 00:03
加一句double Yuebao::profitRate = 10;位置在类声明后面

具体原因我也不太清楚,因为我也是新手,可能静态成员变量必须自己初始化吧。
#3
jinge10012016-12-11 09:12
回复 2楼 yangwawa
静态成员如果没有初始化会自动初始化为0啊
#4
rjsp2016-12-12 08:24
以下是引用jinge1001在2016-12-11 09:12:56的发言:

静态成员如果没有初始化会自动初始化为0啊
你说的和二楼说的有关系吗?
你说的是 Yuebao::profitRate 的值,二楼说的是 Yuebao::profitRate 未定义。
#5
jinge10012016-12-13 11:08
回复 2楼 yangwawa
是我记错了,我以为静态数据声明之后自动初始化为0.
1