非静的データメンバー初期化子は、-std = c ++ 11または-std = gnu ++ 11でのみ使用可能です

Aug 22 2020

[Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11

以下では//、コードが正常に機能しているにもかかわらず、エラーが発生した3行のコードを示しています。

#include <iostream>
#include <conio.h>

using namespace std;

class Bank
{
  private:
    char name[20];
    int accNo;
    char x;
    double balance;
    double amount;
    float interestRate;
    float servCharge = 5;  //[Warning]
    float count = 0;  //[Warning] 
    bool status = true;  //[Warning]

  public:
    void openAccount();
    void depositMoney();
    void withdrawMoney();
    void checkBalance_info();
    void calcInt();
    void monthlyProc();
};

void Bank::calcInt() {
cout << " Enter your annual interestRate : " << endl;
cin >> interestRate;

double monthlyInterestRate = interestRate / 12;
double monthlyInterest = balance * monthlyInterestRate;
balance += monthlyInterest;

cout << "Updated Balance After Monthly interestRate " << balance << endl;

if (balance < 25){
   status = true;
}

void Bank :: monthlyProc(){  
  if (balance < 25){
    status = false;
  }   
  while (count > 4){
    balance = balance - 1;
  }
  servCharge = servCharge + (count * 0.10);
  balance -= servCharge;
  cout << "Monthly Service Charges: " << servCharge <<endl;
  cout << "Updated Balance After Monthly interestRate " << balance << endl;
}

また、少し長いので、コード全体を含めませんでした。コード全体をアップロードする必要があるかどうか教えてください。なんらかのエラーなしでコードを実行するには、助けが必要です。

回答

3 artm Aug 23 2020 at 04:00
float servCharge = 5; //[Warning]

float count = 0;//[Warning] 

bool status = true;//[Warning]

これらは警告であり、エラーではありません。これは、クラス内でこれらのメンバー変数を初期化していますが、静的メンバーではないことを意味します。これは、古いC ++ 98およびC ++ 03の制限でした。

これらの警告は、次の2つの方法で排除できます。

(1)コンパイラーが要求することを正確に実行します。つまり、コードをコンパイルするときに次のオプションを指定します。

-std=c++11 or -std=gnu++11  // using newer C++11

(2)古い方法を使用して初期化する代わりに、それらのクラス内定義を初期化してください。コンストラクターの使用:

Bank::Bank() : servCharge(5), count(0), status(true)
{
   //..
}