The code is as below. Since the number of parameters is different, the call should not be ambiguous. One of the constructors accepts a string and other string plus integer. Why do I get the error?
#include <iostream> #include <string> using namespace std; class AmbiguosCheck { public: AmbiguosCheck(string checkId = string(""), int length = 0) : checkId_(checkId), length_(length){} AmbiguosCheck(string xmlstring) { fromStringForInternalTransfer(xmlstring); } string checkId_; int length_; string toStringForInternalTransfer(){ return checkId_ + "|" + to_string(length_); } void fromStringForInternalTransfer(string xmlstring) { checkId_ = xmlstring; //Using simple assigment for sample code. Need to split, convert and assign the values. length_ = 20; } }; int main() { AmbiguosCheck bd((string)"Check ID|20.000"); } Compilation error:
In function 'int main()': 35:44: error: call of overloaded 'AmbiguosCheck(std::string)' is ambiguous 35:44: note: candidates are: 14:5: note: AmbiguosCheck::AmbiguosCheck(std::string) 9:5: note: AmbiguosCheck::AmbiguosCheck(std::string, int) 6:7: note: AmbiguosCheck::AmbiguosCheck(const AmbiguosCheck&) 6:7: note: AmbiguosCheck::AmbiguosCheck(AmbiguosCheck&&) And why are there 2 more candidates for ambiguity?
std::stringargument (the one taking anintargument have both thestd::stringandintarguments as optional). Which one should be used?