operator-keyword

substituting __iadd__ doesn't work as expected for += operator

≡放荡痞女 提交于 2019-12-12 19:13:58
问题 I have an object from a library (numpy.ndarray), in which I've substituted the __iadd__ method for a custom one. If I call object.__iadd__(x), it works as expected. However, object+=x seems to call the old (unsubstituted) method. I wanted to prevent overflows on numpy from occurring on specific cases, so I created a context manager for that. Here's the (still very crude) code: class NumpyOverflowPreventer( object ): inverse_operator= { '__iadd__':'__sub__', '__isub__':'__add__', '__imul__': '

How do I fix “ambiguous overload” error when overloading operator<< (templated)?

假如想象 提交于 2019-12-12 13:34:26
问题 I am trying to overload the << operator, but I get the following error: error: ambiguous overload for 'operator<<' in 'std::cout << "Test "' ..Followed by 5 billion other errors similar to: c:\mingw\bin../lib/gcc/mingw32/4.5.2/include/c++/ostream:165:7: note: candidates are: ... This comes up because I'm using cout in my main.cpp file. Here is my code: In BinTree.h: template <typename T> class BinTree{ ... friend std::ostream& operator<< <>(std::ostream&, const T&); In BinTree.cpp: template

Scala's equivalence to |> in F# or ->> in Clojure

那年仲夏 提交于 2019-12-12 13:17:45
问题 In Scala, when I have this expression f1 ( f2 ( f3 (p))) Is there a way that I can use something like in F# p |> f3 |> f2 |> f1 or Clojure? (->> p f3 f2 f1) 回答1: If you want to write it yourself without using external libraries, implicit class Pipe[T](x: T) { def |> [U](f: T=>U): U = f(x) } So, this implicit class pattern is used for extension methods. It's shorthand for the "pimp my library" pattern: class Pipe[T](x: T) { /*extension methods here*/ } implicit def anyToPipe[T](x: T) = new

Is it possible to write auto-cast operator outside a struct?

心已入冬 提交于 2019-12-12 07:34:22
问题 The exact situation is next: I have defined in system API structs CGPoint and CGSize , and I want to be able to write my_point = my_size . I can't modify CGPoint struct, only can write external operator. I can write binary operators ( + , - , ...) but operator= must by declared inside struct. So is there any other solution? 回答1: To make the expression a = b; compile you need to either have an operator= in the type of a that takes an element of the type of b , or a type implicitly convertible

MySQL - Duplicate columns after using Join operator

放肆的年华 提交于 2019-12-12 04:57:17
问题 As stated in the title, I'm getting duplicate columns with this JOIN query. A few tables are given and I want to write select statements to get only the information from the tables which are needed. Here is my SQL code so far: SELECT mitarbeiter.PNR, pfleger.PNR, Name from pfleger JOIN mitarbeiter on (mitarbeiter.PNR=pfleger.PNR) where Ort='Frankfurt'; After executing, I get the following result: You can see the problem: I have two PNR columns which I don't want to have. How can I remove the

Linq search text using 'and' operator

你说的曾经没有我的故事 提交于 2019-12-12 04:15:36
问题 I have a one to many relationship in the following tables Products [ProductId, Name(varchar)] Keywords [KeywordId, Keyword(varchar)] KeywordsToProducts [Id, ProductId, KeywordId] Let's say that search text is "blue pro". I need to search for both keywords using operator 'and'. If I do the following: string test="blue pro"; string[]words = test.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var query = from p in Products join kp in KeywordsToProducts on p.ProductId equals kp

PHP bug with ternary operator?

醉酒当歌 提交于 2019-12-12 04:13:19
问题 function foo() { return $result = bar() ? $result : false; } function bar() { return "some_value"; } foo(); Notice: Undefined variable: result Is this a bug? bar() should be saved to $result, but it doesn't. However the condition works fine and it's trying to return $result or false statement (in case bar() is NULL or false) PHP 5.4.24 回答1: That's because operators precedence. Do function foo() { return ($result = bar()) ? $result : false; } -so assignment will be evaluated with higher

overloading a simple = operator

淺唱寂寞╮ 提交于 2019-12-12 03:57:32
问题 I am trying to overload part of my class to a string and I can't get the overloading to work. Alternatively, I will also have a long long overload, but I just assume that it will be the same excepted for long long instead of string . class FileData { public: string extensions_; unsigned long long containsBytes_; }; string& operator = (string& s , FileData& fd) { s= fd.extensions_; return s; } string extName = fileVector[0]; The error I keep getting is ERROR:'operator=' must be a member

How to modify a given class to use const operators

时间秒杀一切 提交于 2019-12-12 02:36:59
问题 I am trying to solve my question regarding using push_back in more than one level. From the comments/answers it is clear that I have to: Create a copy operator which takes a const argument Modify all my operators to const But because this header file is given to me there is an operator what I cannot make into const. It is a simple: float & operator [] (int i) { return _item[i]; } In the given program, this operator is used to get and set data. My problem is that because I need to have this

overloading operator<< to change member variable

守給你的承諾、 提交于 2019-12-12 02:09:18
问题 I have a class that looks something like this: class A { public: A() {}; A& operator<< (A& a, unsigned int i); private: vector<int> abc; } I want to offer the ability to add objects to abc using an operator: A a(); a << 3,4,5; // should do the same as several calls of abc.push_back(i) I know that I have to overload the << operator, do I also have to overload the , operator? How would the actual method look like? 回答1: If you want to keep the aspect of a << 1, 2, 3 you could change your