Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
981 views
in Technique[技术] by (71.8m points)

error: ISO C++ forbids in-class initialization of non-const static member

this is the header file: 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

      lastName(last) 

lastName overloaded constructor

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

Returns the first and last name of each object

        --counter; 

Counter minus one

    }

    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

#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

    { 

Start a new scope block

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

Initialize the e1 object from Employee class

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

Initialize the e2 object from Employee class

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

        cout << "

Employee 1: " << e1.getFirstName() << " " << e1.getLastName()
             << "
Employee 2: " << e2.getFirstName() << " " << e2.getLastName()
             << "

";
    } 

end the scope block

    cout << "
NUmber 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...