overload

C++ Overload Static Function with Non-Static Function

匿名 (未验证) 提交于 2019-12-03 01:59:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I would like to print two different things depending on whether a function is called statically with Foo::print() or from an instance of Foo foo; foo.print(); EDIT: Here is a class definition that definitely does not work, as answered by a few people already. class Foo { string bla ; Foo () { bla = "nonstatic" ; } void print () { cout << bla << endl ; } static void print () { cout << "static" << endl ; } }; However, is there a good way of achieving this effect? Basically, I would like to do: if ( this is a static call ) do one

Missing const_iterator overload of std::vector::erase() with g++ 4.8

匿名 (未验证) 提交于 2019-12-03 01:58:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: The following example will not compile using g++ 4.8.2: #include #include using namespace std ; int main () { vector v { 1 , 2 , 3 }; v . erase ( v . cbegin ()); // Compiler complains return 0 ; } The compiler says the following. (It isn't very readable, but it's complaining that there's not a known conversion between vector ::const_iterator and vector ::iterator .) prog . cpp : In function ‘ int main ()’: prog . cpp : 8 : 20 : error : no matching function for call to ‘ std :: vector :: erase ( std :: vector :: const_iterator )’ v

Why can some operators only be overloaded as member functions, other as friend functions and the rest of them as both?

匿名 (未验证) 提交于 2019-12-03 01:49:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Why can some operators only be overloaded as member functions, other as non-member "free" functions and the rest of them as both? What is the rationale behind those? How to remember which operators can be overloaded as what (member, free, or both)? 回答1: The question lists three classes of operators. Putting them together on a list helps, I think, with understanding why a few operators are restricted in where they can be overloaded: Operators which have to be overloaded as members. These are fairly few: The assignment operator=() .

no overload for method “ToString” takes 1 arguments

匿名 (未验证) 提交于 2019-12-03 01:44:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a data reader to read the datas from database. I am reading TotalPrice from sales table. I would like to show the total price as 2 decimal place. The code is something link that: TotalPrice.Text = myReader["TotalPrice"].ToString("N2"); However i encounted this error: no overload for method "ToString" takes 1 arguments What's wrong with the code? 回答1: Assuming that TotalPrice is a Decimal column, and that myReader is a DataReader: TotalPrice.Text = myReader.GetDecimal(myReader.GetOrdinal("TotalPrice")).ToString("N2"); The idea here is

Why am i getting “No overload method for Add takes 1 argument” when adding a Dictionary to a List of Dictionaries

匿名 (未验证) 提交于 2019-12-03 01:33:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Sorry if this is basic. I am a little new to C#, but why cant I add a Dictionary to the list of Dictionaries? The documentation I have looked up does it like this: List<Dictionary<string, string>> data = new List<Dictionary<string, string>>(); data.Add(new Dictionary<string, string>() { "test", "" }); 回答1: When you create your Dictionary<string, string> in the second line, you are not initializing it correctly. What you need is this: List<Dictionary<string, string>> data = new List<Dictionary<string, string>>(); data.Add(new Dictionary

How do I overload the square-bracket operator in C#?

匿名 (未验证) 提交于 2019-12-03 01:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: DataGridView, for example, lets you do this: DataGridView dgv = ...; DataGridViewCell cell = dgv[1,5]; but for the life of me I can't find the documentation on the index/square-bracket operator. What do they call it? Where is it implemented? Can it throw? How can I do the same thing in my own classes? ETA: Thanks for all the quick answers. Briefly: the relevant documentation is under the "Item" property; the way to overload is by declaring a property like public object this[int x, int y]{ get{...}; set{...} } ; the indexer for DataGridView

C++ Operator += overload

匿名 (未验证) 提交于 2019-12-03 01:26:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want to overload the operator += in the way that when i will use it a+=b; it will add b to the vector a having this in the header: public: ... void Book::operator+=(std::string a, std::vector<std::string>>b); private: ... std::string b; stf::vector<std::string> a; this is the implementation in cpp void Book::operator+=(std::string a, std::vector<std::string>>b) { b.push_back(a); } What can be my error? It is not clear for me the use of overload operators yet 回答1: You can overload the += operator using a member function or a non-member

C#6&#039;s Improved overload resolution - clarification?

匿名 (未验证) 提交于 2019-12-03 01:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Among all the new features in C#6, the most mysterious feature (to me) is the "improved overload resolution" . Maybe it's because I couldn't find related info/examples/explanation about it. The only two remaining features not discussed are support for defining a custom Add extension method to help with collection initializers, and some minor but improved overload resolution Looking at the roslyn wiki And so I ask: Question : How exactly do the Improved overload resolution comes into play in C#6? And how it is different from C#5 (Example?

Overload a C++ function according to the return value

匿名 (未验证) 提交于 2019-12-03 01:18:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: We all know that you can overload a function according to the parameters: int mul(int i, int j) { return i*j; } std::string mul(char c, int n) { return std::string(n, c); } Can you overload a function according to the return value? Define a function that returns different things according to how the return value is used: int n = mul(6, 3); // n = 18 std::string s = mul(6, 3); // s = "666" // Note that both invocations take the exact same parameters (same types) You can assume the first parameter is between 0-9, no need to verify the input or

error TS2384: Overload signatures must all be ambient or non-ambient

匿名 (未验证) 提交于 2019-12-03 01:00:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to configure a new webpack + angular2 project and I'm getting some errors: When I use "npm start", I got a lot of errors like this: ERROR in ./~/reflect-metadata/Reflect.ts (953,21): error TS2384: Overload signatures must all be ambient or non-ambient. ERROR in ./~/reflect-metadata/Reflect.ts (985,21): error TS2384: Overload signatures must all be ambient or non-ambient. ERROR in ./~/reflect-metadata/Reflect.ts (1021,21): error TS2384: Overload signatures must all be ambient or non-ambient. ERROR in /home/gchiara/desenvolvimento