c++ - 非静态数据成员初始化器仅适用于 -std=c++11 或 -std=gnu++11
问题描述
[Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11
下面我曾经//
展示了我得到错误的三行代码,尽管代码工作正常。
#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;
}
另外,我没有包含整个代码,因为它有点长。请告诉我是否需要上传整个代码。只需要帮助以使代码运行而不会出现任何错误。
解决方案
float servCharge = 5; //[Warning]
float count = 0;//[Warning]
bool status = true;//[Warning]
这些是警告,而不是错误。这意味着您正在类中初始化这些成员变量,但它们不是静态成员。这是旧 C++98 和 C++03 的限制。
您可以通过两种方式消除这些警告:
(1) 完全按照编译器的要求执行,即在编译代码时指定这些选项:
-std=c++11 or -std=gnu++11 // using newer C++11
(2) 初始化那些类内定义,而不是使用旧方式初始化它们,即。使用构造函数:
Bank::Bank() : servCharge(5), count(0), status(true)
{
//..
}
推荐阅读
- php - 如何在 php 文件中包含静态资源,如图像、css 和 javascript
- sql - 针对 dbms 问题改进 sql 查询
- firebase - Firebase 事件发生时更新反应原生(博览会)UI
- python - 如何仅打印此输出的最后一行?
- r - 使用 read_html 在 R 中读取时处理 404 和其他错误 URL
- excel - 在 Excel VBA 中删除条件
- python - 在 sqlalchemy 中插入父对象时插入子对象列表
- javascript - Discord Bot 不会通过 node 上线。或节点 main.js
- google-apps-script - 如何使用应用程序脚本获取范围内所有单元格的背景颜色并将背景设置为相同大小的不同范围?
- shell - 删除 shell 脚本中的最后一个参数 (POSIX)