std::to_string - more than instance of overloaded function matches the argument list
In VC++ 2010 there are three overloads of std::to_string
that take long long
, unsigned long long
, and long double
, respectively – clearly int
is none of these, and no one conversion is better than another (demo), so the conversion cannot be done implicitly/unambiguously.
In terms of real C++11 support, this is a failing on the part of VC++ 2010's standard library implementation – the C++11 standard itself actually calls for nine overloads of std::to_string
([string.conversions]/7):
string to_string(int val);
string to_string(unsigned val);
string to_string(long val);
string to_string(unsigned long val);
string to_string(long long val);
string to_string(unsigned long long val);
string to_string(float val);
string to_string(double val);
string to_string(long double val);
Had all of these overloads been present, you obviously wouldn't have this problem; however, VC++ 2010 wasn't based on the actual C++11 standard (which did not yet exist at the time of its release), but rather on N3000 (from 2009), which does not call for these additional overloads. Consequently, it's harsh to blame VC++ too much here...
In any case, for only a handful of calls, there's nothing wrong with using a cast to resolve the ambiguity yourself:
void SentryManager::add(std::string& name, std::shared_ptr<Sentry>) {
name += std::to_string(static_cast<long long>(counter));
}
Or, if there's heavy usage of std::to_string
in your codebase, write a few wrappers and use those instead – this way, no call-site casting is needed:
#include <type_traits>
#include <string>
template<typename T>
inline
typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value, std::string>::type
to_string(T const val) {
return std::to_string(static_cast<long long>(val));
}
template<typename T>
inline
typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value, std::string>::type
to_string(T const val) {
return std::to_string(static_cast<unsigned long long>(val));
}
template<typename T>
inline typename std::enable_if<std::is_floating_point<T>::value, std::string>::type
to_string(T const val) {
return std::to_string(static_cast<long double>(val));
}
// ...
void SentryManager::add(std::string& name, std::shared_ptr<Sentry>) {
name += to_string(counter);
}
I can't check whether VC++ 2010 succeeds or fails with the above usage of SFINAE; if it fails, the following – using tag dispatch instead of SFINAE – should be compilable (if potentially less clear):
#include <type_traits>
#include <string>
namespace detail {
template<typename T> // is_float is_unsigned
inline std::string to_string(T const val, std::false_type, std::false_type) {
return std::to_string(static_cast<long long>(val));
}
template<typename T> // is_float is_unsigned
inline std::string to_string(T const val, std::false_type, std::true_type) {
return std::to_string(static_cast<unsigned long long>(val));
}
template<typename T, typename _> // is_float
inline std::string to_string(T const val, std::true_type, _) {
return std::to_string(static_cast<long double>(val));
}
}
template<typename T>
inline std::string to_string(T const val) {
return detail::to_string(val, std::is_floating_point<T>(), std::is_unsigned<T>());
}
C++ more than one instance of overloaded function matches the argument list when creating a header file
You have a mismatch between declaration in header and definition (which also serves as declaration):
void robotComplexity(vector<Part> vecB, vector<Customer> vecC);
void robotComplexity(const vector<Part>& vecB, const vector<Customer>& vecC);
Whereas parameter names can mismatch, types shouldn't, else, you create another overload.
More than one instance of overloaded function matches the argument list and I can't find where the error happens
As the comments by cigien and Peter already pointed out: The declaration and the definition of getname()
have mismatched parameters. To fix that, change the line
void getname(ofstream);
to
void getname(ofstream&);
Note the &
after ofstream
.
Furthermore, any function that gets an ofstream
as parameter should get that by reference (i. e. as ofstream&
and not just ofstream
), because there is no copy constructor for ofstream
and any attempt to pass ofstream
by value will cause compile errors.
to_string(42) more than one instance matches the argument
Visual C++ 2010 has only three overloads for std::to_string
that take long long
, unsigned long long
, and long double
. The standard defines more than that, but VC++ 2010 doesn't support them. No conversion from the int
literal 42
is preferred, so the call is ambiguous. Instead, you can use a different type of integer literal. For example:
std::string s = std::to_string(42LL);
no instance of overloaded function stoi matches the argument list
std::stoi
expects a std::string
as argument, but you are giving it a single char
.
There is no direct conversion from char
to std::string
, so you need to be explicit about it:
stoi(string(1, ch1));
Here string(1, ch1)
creates a string of length 1
containing only the character ch1
.
Alternatively, if you are sure that ch1
is a digit at that point (stoi
will throw if it isn't) you can simply subtract '0'
, since the digits are guaranteed to be correctly ordered in the character set:
ch1 - '0'
Or rather, you probably want to pass a std::string
directly to your function, instead of multiple individual char
s. You can use the .substr
member function to get substrings from a string.
No instance of overloaded function matches the argument list, argument types are: (std::ifstream, std::string, char)
Your problem is that your accessors don't return string&, but a new copy of a string that cannot be passed as a string&.
string& getItemId() { return itemId; }
string& getTitle() { return title; }
string& getType() { return type; }
string& getLoan() { return loan; }
string& getCopy() { return num_copy; }
string& getFee() { return fee; }
string& getGenre() { return genre; }
On top of that, don't use using namespace std
, use std::move
in your constructor, don't use exit
but throw an exception...
Related Topics
Override Compile Flags for Single Files
C++ Singleton VS. Global Static Object
How to Use _Crt_Secure_No_Warnings
How to Create a Function Dynamically, During Runtime in C++
Return a "Null" Object If Search Result Not Found
How to Get the Gl Library/Headers
Fast Fixed Point Pow, Log, Exp and Sqrt
Cin.Ignore(Numeric_Limits<Streamsize>::Max(), '\N')
How to Erase Elements from Stl Containers
Opencv Multi Channel Element Access
Is Using #Pragma Warning Push/Pop the Right Way to Temporarily Alter Warning Level
What Are the Differences Between -Std=C++11 and -Std=Gnu++11
Why Does C++ Not Allow Inherited Friendship
Does Std::Mt19937 Require Warmup
Overriding Non-Virtual Methods
Trial-Division Code Runs 2X Faster as 32-Bit on Windows Than 64-Bit on Linux