Note: constructor error prone; Note: constructor error prone

Source: Internet
Author: User

Note: constructor error prone; Note: constructor error prone

This article describes the problems that may occur when no default constructor is available in the class and how to avoid them.
The following section may cause errors during program compilation.

1 # include <iostream> 2 3 # include "string. h "4 using namespace std; 5 6 class Student 7 {8 private: 9 int Num, Chinese, Maths, English; 10 float Total; 11 char Name [20]; 12 public: 13 // Student () {}// default constructor 14 Student (char name [20], int num, int chinese, int maths, int english); 15 16 }; 17 Student: Student (char name [20], int num, int chinese, int maths, int english) {18 int n; 19 for (n = 0; n <strlen (name); n ++) 20 Name [n] = name [n]; 21 Num = num; 22 Chinese = chinese; 23 English = english; 24 Maths = maths; 25 Total = maths + chinese + english; 26} 27 28 int main () 29 {30 int I, j; 31 int num, chinese, maths, english; 32 char name [20]; 33 Student std [5]; // compilation fails at this time 34 35}
View Code

The error message is as follows:

[Error] no matching function for call to 'student: Student ()'

To better illustrate the cause and solution of this error, the following describes the meaning of default initialization.

Default initialization:When the defined variable is not initialized, the compiler will automatically initialize it. This process is called default initialization, which is determined by the type and location of the variable.If it is a built-in type of global variables (int float ...) the value is initialized to 0, but the local variables in the function are not initialized to 0. Therefore, pay attention when using local variables.

Now let's talk about how the compiler initializes classes by default:

The default class initialization is determined by the default constructor, that isThe compiler automatically calls the default constructor.(Default constructor: constructor without any real parameters). If the programmer does not show a constructor, the compiler automatically and implicitly defines a constructor (the default constructor for synthesis ).When a class object is defined, it first calls the constructor for initialization.(This is some built-in types such as int float... none), unless you display the call initialization work (that is, the constructor you designed), the system calls the constructor without parameters by default, because no constructor without parameters is defined in the Code, a call error occurs, that is, a compilation error occurs..After the constructor is defined, the default constructor synthesized by the compiler does not work. So there is no default constructor in the Student class.,

That is, there is no constructor without parameters. So Student std [5]; at this time, the system calls the default constructor, and the default constructor does not exist at this time, an error will occur.

Solution: when creating a class, you can create a default constructor in addition to your own constructor. Or just create your own constructor, but you should pay attention to initialization when defining variables.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.