C++ 错误:ISO C++;禁止类内初始化非常量静态成员
这是头文件:employee.hC++ 错误:ISO C++;禁止类内初始化非常量静态成员,c++,static,compiler-errors,standards,iso,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),
#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),
lastName(last)
lastName重载构造函数
{ //The constructor start
++counter;
它为每个创建的对象添加一个加号
cout << "Employee constructor for " << firstName
<< ' ' << lastName << " called." << endl;
}
~Employee() {
计数器负一
}
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");
cout << "\nNUmber of employees after objects are deleted is "
<< Employee::getCount() << endl; //shows the counter's value
} //End of Main
从Employee类初始化e1对象
Employee e2("Robert", "Jones");
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";
}
从Employee类初始化e2对象
Employee e2("Robert", "Jones");
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静态成员计数器的初始化不能在头文件中
将头文件中的行更改为
static int counter;
并将以下行添加到employee.cpp:
int Employee::counter = 0;
原因是将这样的初始化放在头文件中会在包含头文件的每个地方复制初始化代码 根据需要,还有另一种方法,特别适合您当前的实现(仅标题库):
我冒昧地将其用于表示非负员工计数和函数
附带测试():
#包括“Employee.h”
int main(){
std::难道我不认为这是他想要的,因为计数器稍后会被修改。@PMF:是的,你是对的!我想要初始化计数器,然后用构造函数递增。然后用析构函数递减。我认为错误消息再清楚不过了。@texasbruce错误消息只告诉你出了什么问题,而不是如何解决问题问题。我这样做了,但它返回:在函数'int main()中“:错误:“int Employee::counter”是私有错误:在此context@user3053929:你把它放在哪里了?它需要在任何函数之外,但类Employee是已知的。顺便问一下:为什么你的cpp文件名为employee2.cpp?并包括employee2.h?因为我做了另一个。但这并不重要。谢谢你的信息,如果这是唯一的原因的话son为什么会被阻止,为什么会允许常量成员?它们不是在头包含的每个位置都会被初始化吗?@JesuScrist:不确定。常量成员是否每次都有一个新的内存位置(事实上存在多次)或者根本没有(因为编译器会消除它)
// 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
#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;
}