Home > Backend Development > C++ > body text

C++ error: Non-static data members must be initialized, how to modify it?

PHPz
Release: 2023-08-22 17:45:13
Original
1207 people have browsed it

C++ error: Non-static data members must be initialized, how to modify it?

In C programming, when we define non-static data members of a class, if they are not initialized in the constructor or initialization list, the following error will occur: Must When a non-static data member is initialized, this will cause the compiler to be unable to allocate memory space for the member and thus fail to instantiate the class.

To solve this problem, we can take the following measures:

1. Initialize the member variables in the constructor

If we do not initialize the member variables when defining the class To initialize, you can initialize it in the constructor, and this approach is more flexible in some cases. For example:

class Person{
public:
    Person() {
        age = 18;
        name = "Tom";
    }

private:
    int age;
    string name;
};
Copy after login

In the above code, we did not initialize the age and name member variables when defining the Person class, but initialized them in the constructor. It should be noted that if there are multiple constructors in a class, all constructors need to initialize member variables.

2. Initialize in the member initialization list

In C, the member initialization list is a method used to initialize non-static member variables of a class. Non-static member variables in a class can be initialized quickly and clearly by adding a colon after the constructor name and setting the member variables and their initialization expressions in the member initialization list after the constructor. For example:

class Person{
public:
    Person(): age(18), name("Tom"){}

private:
    int age;
    string name;
};
Copy after login

In the above code, we use the member initialization list to initialize the age and name member variables.

It should be noted that if the type of the member variable is a user-defined type, you need to ensure that the type has a default constructor. Otherwise, the compiler will not be able to find a suitable constructor when initializing using a member initializer list.

Summary

C requires that non-static data members of a class must be initialized when defining them, otherwise there will be an error that non-static data members must be initialized. To solve this problem, we can initialize the member variables in the constructor or use a member initialization list to initialize them. Either way, make sure all member variables are initialized.

The above is the detailed content of C++ error: Non-static data members must be initialized, how to modify it?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!