Reputation: 103
I am learning c++. I made this program but on compiling it, the program shows ambigious error. I don't understand that if i am making an object with no arguments then it should only call default constructor & the program should run without any error. Here is the code:
#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;
}
I have tried this code on compiler like Codeblocks, Dev c++ & GCC also.
Upvotes: 0
Views: 491
Reputation: 47824
room r1
is ambiguous because a constructor with all parameters defaulted is already available
as room()
as a default constructor
§ 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).
Upvotes: 3
Reputation: 169
You have 3 constructors which could be called without supplying any arguments. So the compiler gets confused by these 3 constructors.
Upvotes: 0