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

mis*_*ala 17 c++ standards static iso compiler-errors

这是头文件: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) 
Run Code Online (Sandbox Code Playgroud)

重载的构造函数

    : firstName(first), 
Run Code Online (Sandbox Code Playgroud)

firstName重载的构造函数

      lastName(last) 
Run Code Online (Sandbox Code Playgroud)

lastName重载的构造函数

    { //The constructor start
    ++counter; 
Run Code Online (Sandbox Code Playgroud)

它为每个创建的对象增加一个加号;

    cout << "Employee constructor for " << firstName
         << ' ' << lastName << " called." << endl;
    }

    ~Employee() { 
Run Code Online (Sandbox Code Playgroud)

析构函数cout <<"~Workee()调用"<< firstName <<"<< lastName << endl;

返回每个对象的名和姓

        --counter; 
Run Code Online (Sandbox Code Playgroud)

反减一

    }

    string getFirstName() const {
        return firstName; 
    }

    string getLastName() const {
        return lastName;
    }

    static int getCount() {
        return counter;
    }
private:
    string firstName;
    string lastName;

   static int counter = 0; 
Run Code Online (Sandbox Code Playgroud)

这是我得到错误的地方.但为什么?

};
Run Code Online (Sandbox Code Playgroud)

主要计划:employee2.cpp

#include <iostream>
#include "employee2.h"
using namespace std;

int main()
{
    cout << "Number of employees before instantiation of any objects is "
         << Employee::getCount() << endl; 
Run Code Online (Sandbox Code Playgroud)

在这里,我可以从班级中拨打计数器的价值

    { 
Run Code Online (Sandbox Code Playgroud)

启动一个新的范围块

        Employee e1("Susan", "Bkaer"); 
Run Code Online (Sandbox Code Playgroud)

从Employee类初始化e1对象

        Employee e2("Robert", "Jones"); 
Run Code Online (Sandbox Code Playgroud)

从Employee类初始化e2对象

        cout << "Number of employees after objects are instantiated is"
             << Employee::getCount(); 

        cout << "\n\nEmployee 1: " << e1.getFirstName() << " " << e1.getLastName()
             << "\nEmployee 2: " << e2.getFirstName() << " " << e2.getLastName()
             << "\n\n";
    } 
Run Code Online (Sandbox Code Playgroud)

结束范围块

    cout << "\nNUmber of employees after objects are deleted is "
         << Employee::getCount() << endl; //shows the counter's value
} //End of Main
Run Code Online (Sandbox Code Playgroud)

问题是什么?我不知道出了什么问题.我一直在思考,但是我没有错.

PMF*_*PMF 55

静态成员的初始化counter不得位于头文件中.

将头文件中的行更改为

static int counter;
Run Code Online (Sandbox Code Playgroud)

并将以下行添加到employee.cpp:

int Employee::counter = 0;
Run Code Online (Sandbox Code Playgroud)

原因是在头文件中放置这样的初始化会在包含头的每个地方复制初始化代码.


muc*_*aho 6

根据类似的 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
Run Code Online (Sandbox Code Playgroud)

我冒昧地使用函数std::size来表示非负员工计数和尾随返回语法

附带测试(ideone 链接):

#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;
}
Run Code Online (Sandbox Code Playgroud)