Search code examples
c++constructorcompiler-errorsdefault-parameters

Constructor default parameter


Constructors:

A()
{
    std::cout<<"In A const";
}

A(int a = 3)
{
    std::cout<<"In a with default :"<<a;
}

Creating objects:

A a;
A a1(4);

The above code is showing error: call of overloaded 'A()' is ambiguous


Solution

  • You've given a default value to the second constructor:

    A(int a = 3)
    

    So this covers both instances of: A() and A(3), making the first definition redundant