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

时间:2013-12-01 07:56:34

标签: c++ static compiler-errors standards iso

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

lastName重载构造函数

    { //The constructor start
    ++counter; 

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

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

    ~Employee() { 

析构             cout&lt;&lt; &#34;〜员工()呼吁&#34; &LT;&LT; firstName&lt;&lt; &#39; &#39;                  &LT;&LT; lastName&lt;&lt; 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; 

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

};

主程序: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; 

这里可以从课程中调用计数器的值

    { 

启动新的范围块

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

从Employee类

初始化e1对象
        Employee e2("Robert", "Jones"); 

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

结束范围块

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

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

2 个答案:

答案 0 :(得分:47)

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

将标题文件中的行更改为

static int counter;

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

int Employee::counter = 0;

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

答案 1 :(得分:1)

根据a similar SO answer,还有另一种方法,特别适合您当前的实现(仅限标题库):

// 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来表示非负的员工数,并使用trailing return syntax来表示职能。

伴随测试(ideone link):

#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;
}