Home  >  Article  >  Backend Development  >  C++ error: The constructor must be declared in the public area, how to deal with it?

C++ error: The constructor must be declared in the public area, how to deal with it?

王林
王林Original
2023-08-21 20:26:461849browse

In C programming, the constructor is an important function used to initialize the member variables of a class. It is automatically called when an object is created to ensure proper initialization of the object. The constructor must be declared in the class, but sometimes you will encounter the error message "The constructor must be declared in the public area."

This error is usually caused by the wrong access modifier of the constructor. In C, class member variables and member functions have an access modifier, including public, private and protected. Public means that the member can be accessed inside and outside the class, private means that it can only be accessed within the class, and protected means that it can only be accessed within the class and its derived classes.

In the definition of the class, if the access modifier declared by the constructor is not public, the compiler will display an error message "The constructor must be declared in the public area". Therefore, to solve this problem, you only need to change the access modifier of the constructor to public.

The following is a sample code:

class Student {
 private:
  string name;
  int age;

 public:
  Student(string n, int a) {
    name = n;
    age = a;
  }

  void display() {
    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
  }
};

int main() {
  Student s("Tom", 18);
  s.display();
  return 0;
}

In the above code, the constructor is defined as a public member function of the class, so that it can be accessed and called outside the class. If the accessibility of the constructor is set to private or protected, an error message "The constructor must be declared in the public area" will be prompted.

In addition to changing the access permission of the constructor to public, we can also use the access permission abbreviation in the definition of the class:

class Student {
  string name;
  int age;

 public:
  Student(string n, int a) {
    name = n;
    age = a;
  }

  void display() {
    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
  }
};

In this way, the private and public keywords can be omitted and the Constructors are set to public by default.

In short, when you encounter the error "The constructor must be declared in the public area", you should first check whether the access modifier of the constructor is public, and follow C's access rights rules to ensure that the class Member variables and member functions can be accessed and called correctly.

The above is the detailed content of C++ error: The constructor must be declared in the public area, how to deal with it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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