-1

次のコードの期待される結果は 505.5 ですが、代わりに 3.97541e+70 が返されます。これはなぜですか?また、どのように問題を解決できますか?

#include <iostream>
#include <string>
using namespace std;
class Position {
public:
    Position(int s, double p, string n) {
        shares = s;
        price = p;
        name = n;
    }
    double getBpEffect() {
        return bpEffect;
    }
private:
    string name;
    int shares;
    double price;
    double bpEffect = (shares*price) / 2;

};


int main() {
    Position xyz = Position(100, 10.11, "xyz");
    double buyingPower = xyz.getBpEffect();


    cout << buyingPower;

    system("pause");
    return 0;

}
4

2 に答える 2

2

double bpEffect = (shares*price) / 2;およびの未定義の値を使用して、コンストラクターの本体のに実行されます。他の変数を初期化した後に計算する必要があります。sharespricebpEffect

于 2016-10-23T00:59:52.503 に答える