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

时间:2022-04-16 22:45:40

this is the header file: employee.h

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

Overloaded Constructor

重载的构造函数

    : firstName(first), 

firstName overloaded constructor

firstName重载构造函数

      lastName(last) 

lastName overloaded constructor

lastName重载构造函数

    { //The constructor start
    ++counter; 

it adds one plus per each object created;

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

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

    ~Employee() { 

Destructor cout << "~Employee() called for " << firstName << ' ' << lastName << endl;

析构函数<< << firstName << << < / span > << / span > < / span > < / span > < / span > < / span > < / span > < / span > < / span > < / span > < / span > < / span > < / p >

Returns the first and last name of each object

返回每个对象的第一个和最后一个名称。

        --counter; 

Counter minus one

反- 1

    }

    string getFirstName() const {
        return firstName; 
    }

    string getLastName() const {
        return lastName;
    }

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

   static int counter = 0; 

Here is where i got the error. But, why?

这里是我得到错误的地方。但是,为什么?

};

principal program: employee2.cpp

主要项目: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; 

Here ir call te counter's value from the class

这里ir调用te counter的值。

    { 

Start a new scope block

启动一个新的范围块。

        Employee e1("Susan", "Bkaer"); 

Initialize the e1 object from Employee class

从Employee类初始化e1对象。

        Employee e2("Robert", "Jones"); 

Initialize the e2 object from Employee class

从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";
    } 

end the scope block

结束了块范围

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

What is the problem? I have no idea what's wrong. I have been thinking a lot, but a i do not what is wrong.

这个问题是什么?我不晓得什么地方不对劲儿。我一直在想,但我不知道怎么回事。

2 个解决方案

#1


40  

The initialization of the static member counter must not be in the header file.

静态成员计数器的初始化不能位于头文件中。

Change the line in the header file to

将头文件中的行更改为。

static int counter;

And add the following line to your employee.cpp:

并将以下内容添加到您的雇员中。

int Employee::counter = 0;

Reason is that putting such an initialization in the header file would duplicate the initialization code in every place where the header is included.

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

#2


1  

According to a similar SO answer there is another approach, in particular suited for your current implementation (header-only library):

根据类似的答案,还有另一种方法,特别适合于当前的实现(header-only库):

// 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

I took the liberty to use std::size for representing the non-negative employee count and trailing return syntax for functions.

我冒昧地使用std::size表示非负雇员计数和函数的后返回语法。

Accompanying test (ideone link):

伴随测试(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;
}

#1


40  

The initialization of the static member counter must not be in the header file.

静态成员计数器的初始化不能位于头文件中。

Change the line in the header file to

将头文件中的行更改为。

static int counter;

And add the following line to your employee.cpp:

并将以下内容添加到您的雇员中。

int Employee::counter = 0;

Reason is that putting such an initialization in the header file would duplicate the initialization code in every place where the header is included.

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

#2


1  

According to a similar SO answer there is another approach, in particular suited for your current implementation (header-only library):

根据类似的答案,还有另一种方法,特别适合于当前的实现(header-only库):

// 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

I took the liberty to use std::size for representing the non-negative employee count and trailing return syntax for functions.

我冒昧地使用std::size表示非负雇员计数和函数的后返回语法。

Accompanying test (ideone link):

伴随测试(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;
}