struct A
{
A();
A(char);
};
A::A(char c = 'A') {}
int main() {
A a;
}
Error message from Visual Studio:
example.cpp
example.cpp(10) : error C2668: 'A::A' : ambiguous call to overloaded function
example.cpp(4): could be 'A::A(char)'
example.cpp(3): or 'A::A(void)'
while trying to match the argument list '(void)'
Perhaps clang's error messages are more self-explanatory
test.cc:7:11: warning: addition of default argument on redeclaration makes this
constructor a default constructor [-Wdefault-arg-special-member]
A::A(char c = 'A') {}
^ ~~~
test.cc:4:2: note: previous declaration was not a special member function
A(char);
^
test.cc:10:4: error: call to constructor of 'A' is ambiguous
A a;
^
test.cc:3:2: note: candidate constructor
A();
^
test.cc:7:4: note: candidate constructor
A::A(char c = 'A') {}
^
void foo() ; // overload 1
void foo( int = 23 ) ; // overload 2
foo() ; // should this call to foo() (overload 1) or should it be foo(23) (overload 2)?
The C++ compiler does not make a wild guess about the what the programmer intented; it throws the problem back at the person who wrote the code - what precisely do you mean by that? The call, as it is written, is ambiguous - there are two possible, equally good, interpretations.
@JLBorges: Right, but the class declares 2 unambiguous constructors. Then I was attempting to define both of them with one single function (with a default parameter.) Is there some specification saying I can't do that? I don't understand how else this could be interpreted (reasonably.)
1) A a; must call a default constructor.
2) class A { A(); } one has been defined.
Now look for it's declaration. (Should have been linked,)
or does something else magical happen at compile time?
@Cubbi: Thanks, I see how it sees both as default constructors, but why? I didn't know the compiler looks for class member declarations outside of the class body, only the definition (to be linked at compile time.)