简体   繁体   English

为什么我的课程只能与两个构造函数一起使用c ++

[英]why does my class only work with two constructors c++

This does not work 这行不通

When I don't have a blank constructor in my class the code will not run causing an error saying no default constructor exists for class. 当我的班级中没有空白的构造函数时,代码将不会运行,并导致错误,指出该类不存在默认构造函数。

#include <iostream>

class myClass
{
public:
    myClass(int val)
        :x(val)
    {}


private:
    int x;
};

int main()
{
    myClass random;
    return 0;
}

This works 这有效

#include <iostream>

class myClass
{
public:
    myClass(int val)
        :x(val)
    {}

    myClass()
    {}

private:
    int x;
};

int main()
{
    myClass random;
    return 0;
}

This is because when you try to instantiate the object myClass random , you are trying to invoke the default constructor which you do not have. 这是因为当您尝试实例化对象myClass random ,您正在尝试调用没有的默认构造函数。

If you changed it to myClass random(3) ( basically trying to invoke the constructor that you have), you would see that the compiler would have no problems. 如果将其更改为myClass random(3) (基本上是尝试调用您拥有的构造函数),则会看到编译器不会有问题。

If you want myClass random to compile fine, then you must have a default constructor in your class. 如果要让myClass random可以正常编译,则您的类中必须具有默认构造函数。

Once you declare a constructor in a class (any constructor), the compiler won't automatically generate a default constructor for you (this is what you're calling the blank constructor). class (任何构造函数)中声明构造函数后,编译器将不会自动为您生成默认构造函数(这就是您所称的空白构造函数)。

If you don't want to implement the default constructor (generally a good idea if you just want the default behavior), you can tell the compiler to generate it for you as of C++11. 如果您不想实现默认的构造函数(如果只希望使用默认行为,通常是个好主意),则可以告诉编译器从C ++ 11开始为您生成它。

class myClass {
public:
    myClass(int val)
        :x(val)
    {}

    myClass() = default; // the compiler handles the implementation

private:
    int x;
};

In the first case you have defined a parameterized constructor. 在第一种情况下,您已经定义了一个参数化的构造函数。 When a constructor is defined the compiler now does not automatically define a default constructor like before. 定义构造函数后,编译器现在不会像以前那样自动定义默认构造函数。

If no constructor is defined the compiler automatically defines a default constructor but if another constructor is defined the compiler will not do so. 如果未定义构造函数,则编译器会自动定义一个默认构造函数,但如果定义了另一个构造函数,则编译器将不会这样做。

ie in first case a default constructor does not exist. 即在第一种情况下,默认构造函数不存在。 In the second case you have defined one and hence has no errors. 在第二种情况下,您已经定义了一个,因此没有错误。

See default constructor . 请参阅默认构造函数

If no user-declared constructors of any kind are provided for a class type, the compiler will always declare a default constructor as an inline public member of its class. 如果没有为类类型提供任何用户声明的构造函数,则编译器将始终将默认构造函数声明为其类的内联公共成员。

However, there's a constuctor declared in your class, thus the compiler won't declare a default constructor. 但是,您的类中声明了一个构造函数,因此编译器不会声明默认构造函数。 You have to explicitly declare one yourself. 您必须自己明确声明一个人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM