error: in-class initialization of static data member
时间: 2024-03-26 09:39:10 浏览: 85
If you get an error message "in-class initialization of static data member" while trying to initialize a static data member inside a class definition, it means that you are using a version of C++ that does not support this feature.
In-class initialization of static data members was introduced in C++11, so if you are using an older version of C++, you will need to initialize your static data member outside the class definition.
Here's an example of how to initialize a static data member outside the class definition:
```c++
class MyClass {
public:
static int myStaticData; // declaration
};
int MyClass::myStaticData = 42; // initialization outside the class definition
```
In this example, we declare the static data member `myStaticData` inside the class definition, and then initialize it outside the class definition using the scope resolution operator `::`.
Note that if you are using a modern version of C++ that supports in-class initialization of static data members, you can still choose to initialize your static data member outside the class definition if you prefer.
阅读全文