错误:ISO C++禁止在类内初始化非 const 静态成员
error: ISO C++ forbids in-class initialization of non-const static member
这是头文件:employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
Employee(const string &first, const string &last)
重载构造函数
: firstName(first),
firstName 重载构造函数
lastName(last)
姓氏重载构造函数
{ //The constructor start
++counter;
它为每个创建的对象添加一个加号;
cout << "Employee constructor for " << firstName
<< ' ' << lastName << " called." << endl;
}
~Employee() {
破坏者 cout <<"~Employee(( 调用" <<firstName <<' ' <<姓氏<<endl;
返回每个对象的名字和姓氏
--counter;
计数器减一
}
string getFirstName() const {
return firstName;
}
string getLastName() const {
return lastName;
}
static int getCount() {
return counter;
}
private:
string firstName;
string lastName;
static int counter = 0;
这是我得到错误的地方。但是为什么?
};
主要项目:员工2.cpp
#include <iostream>
#include "employee2.h"
using namespace std;
int main()
{
cout << "Number of employees before instantiation of any objects is "
<< Employee::getCount() << endl;
这里 ir 从类中调用 te 计数器的值
{
启动新的作用域块
Employee e1("Susan", "Bkaer");
从 Employee 类初始化 e1 对象
Employee e2("Robert", "Jones");
从员工类初始化 e2 对象
cout << "Number of employees after objects are instantiated is"
<< Employee::getCount();
cout << "nnEmployee 1: " << e1.getFirstName() << " " << e1.getLastName()
<< "nEmployee 2: " << e2.getFirstName() << " " << e2.getLastName()
<< "nn";
}
结束作用域块
cout << "nNUmber of employees after objects are deleted is "
<< Employee::getCount() << endl; //shows the counter's value
} //End of Main
问题出在哪里?我不知道出了什么问题。我一直在想很多,但我不知道有什么错。
静态成员counter
的初始化不得在头文件中。
将头文件中的行更改为
static int counter;
并将以下行添加到您的员工.cpp:
int Employee::counter = 0;
原因是将此类初始化放在头文件中会在包含头文件的每个位置复制初始化代码。
根据类似的SO答案,还有另一种方法,特别适合您当前的实现(仅标头库(:
// file "Employee.h"
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
class Employee {
public:
Employee() {
getCounter()++;
}
~Employee() {
getCounter()--;
}
static auto getCount() -> std::size_t {
return getCounter();
}
private:
// replace counter static field in class context,
// with counter static variable in function context
static auto getCounter() -> std::size_t& {
static std::size_t counter = 0;
return counter;
}
};
#endif //EMPLOYEE_H
我冒昧地使用 std::size
来表示函数的非负员工计数和尾随返回语法。
随附的测试(IDONE链接(:
#include "Employee.h"
int main() {
std::cout << "Initial employee count = " << Employee::getCount() << std::endl;
// printed "count = 0"
Employee emp1 {};
std::cout << "Count after an employee created = " << Employee::getCount() << std::endl;
// printed "count = 1"
{
Employee emp2 {};
std::cout << "Count after another employee created = " << Employee::getCount() << std::endl;
// printed "count = 2"
}
std::cout << "Count after an employee removed = " << Employee::getCount() << std::endl;
// printed "count = 1"
return 0;
}
相关文章:
- 如何在旧c++中初始化const-std向量
- 我可以初始化 const 实例,以便我可以将其用作 const 来初始化数组吗?
- 使用 const char* 初始化 const ref 字符串成员时幕后会发生什么
- 如何使用函数的输出初始化 const 数组结构字段?
- 有效地初始化 const std::vector 类成员
- 正在初始化const boost multi_array
- 在类声明中初始化 const 成员变量时在调试模式下出现异常
- 如何在现代C++中用生成器初始化 const 容器?
- 初始化 const 成员的正确方法
- 如何通过头文件中的函数初始化 const int 数组?
- 通过与现有 QHash 连接来初始化 const QHash
- C++ 在头文件或构造函数中初始化 const 类成员变量?
- 在没有构造函数的情况下初始化 const c++ 类
- 如何初始化 const std::vector<MyClass>
- 使用静态方法初始化 const 类字段的做法是好是坏
- 在C++标准中哪里说必须初始化 const 内置类型变量的定义
- 从constructor参数中初始化const多维数组中的const多维数组
- 初始化const 3D数组成员变量
- 在同一构造函数中使用该 const int 初始化 const int 和对象
- 在构造函数中初始化const字段,但首先检查一个参数