错误:ISO C++禁止在类内初始化非 const 静态成员

error: ISO C++ forbids in-class initialization of non-const static member

本文关键字:初始化 const 静态成员 ISO C++ 禁止 错误      更新时间:2023-10-16

这是头文件: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;
}