2021年3月12日星期五

Call to member function is ambigous when overloading both vector of chars and string c++

I want to overload a function the following way:

void f(string s){      cout << "String case" << endl;  }    void f(vector<char> v){      cout << "Vector case" << endl;  }  

For the case I insert a string object or a vector object, everything is fine:

int main()  {      string s = "hello";      f(s); // Prints "String case"      vector<char> v = {'a', 'b'};      f(v); // Prints "Vector case"      return 0;  }  

But if I want to run f({'a', 'b'});, since the array can be automatically converted to both a string and a vector, I get error: call of overloaded 'f()' is ambiguous. Suppose I want to treat this case as a vector, how can I tell the compiler that?

https://stackoverflow.com/questions/66608918/call-to-member-function-is-ambigous-when-overloading-both-vector-of-chars-and-st March 13, 2021 at 08:29AM

没有评论:

发表评论