参数化构造函数的默认值是什么意思?

时间:2014-10-17 06:11:33

标签: c++ constructor

我正在学习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 ++&amp;海湾合作委员会也。

2 个答案:

答案 0 :(得分:3)

room r1不明确,因为默认情况下所有参数的构造函数已经可用 作为room()作为默认构造函数

§12.1

  

类X的默认构造函数是类X的构造函数   可以不带参数调用。如果没有用户声明   类X的构造函数,没有参数的构造函数   隐含声明为违约(8.4)。

答案 1 :(得分:0)

你有3个构造函数,可以在不提供任何参数的情况下调用它们。 因此编译器会被这3个构造函数搞糊涂。

相关问题