error: ISO C ++ prohíbe la inicialización en clase de miembros estáticos no constantes
Este es el archivo de cabecera: 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)
Constructor sobrecargado
: firstName(first),
FirstName sobrecargado constructor
lastName(last)
Constructor sobrecargado de LastName
{ //The constructor start
++counter;
agrega un plus por cada objeto creado;
cout << "Employee constructor for " << firstName
<< ' ' << lastName << " called." << endl;
}
~Employee() {
Destructor cout << "~ Employee () llamado por" << firstName << '' << lastName << endl;
Devuelve el nombre y apellido de cada objeto.
--counter;
Contador menos uno
}
string getFirstName() const {
return firstName;
}
string getLastName() const {
return lastName;
}
static int getCount() {
return counter;
}
private:
string firstName;
string lastName;
static int counter = 0;
Aquí es donde tengo el error. ¿Pero por qué?
};
programa principal: 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;
Aquí te llamamos el valor del contador de la clase.
{
Iniciar un nuevo bloque de alcance
Employee e1("Susan", "Bkaer");
Inicialice el objeto e1 de la clase Employee
Employee e2("Robert", "Jones");
Inicializa el objeto e2 de la clase Employee
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";
}
terminar el bloque de alcance
cout << "\nNUmber of employees after objects are deleted is "
<< Employee::getCount() << endl; //shows the counter's value
} //End of Main
¿Cuál es el problema? No tengo idea de lo que está mal. He estado pensando mucho, pero no sé qué está mal.