overloading

ambiguous call to overloaded function

六月ゝ 毕业季﹏ 提交于 2021-02-20 06:44:07
问题 I have two functions: void DoSomething( const tchar* apsValue ) void DoSomething( size_t aiValue ) Now I want to pass '0' as a size_t: DoSomething(0); The compiler throws an error: "ambiguous call to overloaded function" To solve this, I can use static_cast, for instance: DoSomething(static_cast<size_t>(0)); Or simple: DoSomething(size_t(0)); Is one of them better than the other? Are there any other approaches to solve this? 回答1: It's ambiguous because 0 has type int , not size_t . It can

ambiguous call to overloaded function

百般思念 提交于 2021-02-20 06:44:05
问题 I have two functions: void DoSomething( const tchar* apsValue ) void DoSomething( size_t aiValue ) Now I want to pass '0' as a size_t: DoSomething(0); The compiler throws an error: "ambiguous call to overloaded function" To solve this, I can use static_cast, for instance: DoSomething(static_cast<size_t>(0)); Or simple: DoSomething(size_t(0)); Is one of them better than the other? Are there any other approaches to solve this? 回答1: It's ambiguous because 0 has type int , not size_t . It can

C++ can't find function out of namespace

假装没事ソ 提交于 2021-02-19 02:56:08
问题 Compiling the following code fails because the second function can't find the first one, even though it's outside namespaces. I couldn't figure out the problem myself, and so far I haven't found any answers on the net. test.cpp: #include <bits/stdc++.h> struct myclass {}; template <typename T, typename U> std::ostream& operator<< (std::ostream &os, const std::pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } namespace my { void operator<< (std::ostream os, myclass

overload resolution between lvalue reference and rvalue reference

只愿长相守 提交于 2021-02-18 09:56:26
问题 #include <iostream> using namespace std; void func(int (&ref)[6]) { cout << "#1" << endl; } void func(int * &&ref) { cout << "#2" << endl; } int main() { int arr[6]; func(arr); // g++(5.4): ambiguous, clang++(3.8): #2, vc++(19.11): #1 return 0; } Both functions are exact matches. Below is a quote from the standard: Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if ... S1 and S2 are reference bindings (8.5.3) and neither refers to an

overload resolution between lvalue reference and rvalue reference

旧巷老猫 提交于 2021-02-18 09:55:50
问题 #include <iostream> using namespace std; void func(int (&ref)[6]) { cout << "#1" << endl; } void func(int * &&ref) { cout << "#2" << endl; } int main() { int arr[6]; func(arr); // g++(5.4): ambiguous, clang++(3.8): #2, vc++(19.11): #1 return 0; } Both functions are exact matches. Below is a quote from the standard: Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if ... S1 and S2 are reference bindings (8.5.3) and neither refers to an

Are default parameter values supported by Java? [duplicate]

孤者浪人 提交于 2021-02-18 06:01:30
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Does Java support default parameter values? Suppose I want to make default parameter value in C++, then we can express it as below. void functionName(char *param1, int param2=2); But if I want to make this in Java, then is it possible. Currently I am doing as below public functionName(String param1) { this(param1, 2); } public functionName(String param1, int param2) { .......... } 回答1: It is not possible in Java

Is this an example of python function overload?

被刻印的时光 ゝ 提交于 2021-02-17 07:05:29
问题 I know python does not allow us to overload functions. However, does it have inbuilt overloaded methods? Consider this: setattr(object_name,'variable', 'value') setattr(class_name,'method','function') The first statement dynamically adds variables to objects during run time, but the second one attaches outside functions to classes at run time. The same function does different things based on its arguments. Is this function overload? 回答1: The function setattr(foo, 'bar', baz) is always the

Is this an example of python function overload?

早过忘川 提交于 2021-02-17 07:05:17
问题 I know python does not allow us to overload functions. However, does it have inbuilt overloaded methods? Consider this: setattr(object_name,'variable', 'value') setattr(class_name,'method','function') The first statement dynamically adds variables to objects during run time, but the second one attaches outside functions to classes at run time. The same function does different things based on its arguments. Is this function overload? 回答1: The function setattr(foo, 'bar', baz) is always the

Constructor overload contains already defines a member with the same signature

浪尽此生 提交于 2021-02-17 05:50:28
问题 public Module(string a, object obj) : this(a, null, obj) { } public Module(string b, object obj) : this(null, b, obj) { } These constructor overloads do not work 'already defines a member with same parameter types' I have looked around and realise that I cannot do this in c# but can anyone suggest a way around this? Edit: Thanks for answers. In this case I have decided that to go with this for now: public Module(string a, object obj) : this(a, null, obj) { } public Module(string a, string b,

Constructor overload contains already defines a member with the same signature

大憨熊 提交于 2021-02-17 05:49:33
问题 public Module(string a, object obj) : this(a, null, obj) { } public Module(string b, object obj) : this(null, b, obj) { } These constructor overloads do not work 'already defines a member with same parameter types' I have looked around and realise that I cannot do this in c# but can anyone suggest a way around this? Edit: Thanks for answers. In this case I have decided that to go with this for now: public Module(string a, object obj) : this(a, null, obj) { } public Module(string a, string b,