我正在学习 C++。我制作了这个程序,但在编译它时,程序显示出不明确的错误。 我不明白,如果我正在创建一个没有参数的对象,那么它应该只调用默认构造函数并且程序应该运行而不会出现任何错误。这是代码:
#include<iostream>
using namespace std;
class room
{
int length,width;
public:
room()
{
cout<<"Default constructor";
}
room(int l=0)
{
cout<<"Constructor"; //the compiler is taking this also as default constructor
}
room(int l=0,int w=0)
{
cout<<"Constructor"; //the compiler is taking this also as default constructor on making other two as comment
}
};
int main()
{
room r1;
return 0;
}
我也在 Codeblocks、Dev c++ 和 GCC 等编译器上尝试过这段代码。
最佳答案
room r1
是不明确的,因为所有参数默认的构造函数已经可用
作为 room()
作为默认构造函数
§ 12.1
A default constructor for a class X is a constructor of class X that can be called without an argument. If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted (8.4).
关于c++ - 具有默认值的参数化构造函数是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26418865/