operator-overloading

Swift compiler unable to resolve correct overload of + operator for arrays in generic function

喜欢而已 提交于 2020-01-07 03:19:10
问题 I've had several problems with the plus operator so I decided to investigate and ended up with the following minimal example. When I compile func f<T>(t: T) -> [T] { return [t] + [t] } everything is fine. The compiler chooses this overload of the plus operator: public func +<RRC1 : RangeReplaceableCollection, RRC2 : RangeReplaceableCollection where RRC1.Iterator.Element == RRC2.Iterator.Element> (lhs: RRC1, rhs: RRC2) -> RRC1 However, when I add another + to the game, I get this: func f<T>(t:

Overloaded operator << outputs bool value. why?

拟墨画扇 提交于 2020-01-06 19:36:35
问题 xml_attribute.h #pragma once #ifndef XML_ATTRIBUTET_H #define XML_ATTRIBUTET_H #include <string> #include <iostream> struct XML_AttributeT{ std::string tag; std::string value; //constructors explicit XML_AttributeT(std::string const& tag, std::string const& value); explicit XML_AttributeT(void); //overloaded extraction operator friend std::ostream& operator << (std::ostream &out, XML_AttributeT const& attribute); }; #endif xml_attribute.cpp #include "xml_attribute.h" //Constructors XML

Method Overloading in Objective-C - not used for init?

拜拜、爱过 提交于 2020-01-06 15:18:21
问题 I have just started programming in Objective-C, I understand it only partially supports method overloading due to the way the method names are generated (see this question). However, my question is why I have never seen it used in any examples. The code below seems to work fine, but any sort of example I have seen, the second init would be named initWithServerName or something like that, instead of taking advantage of the overloading. -(id) init { self = [super init]; return self; } //

Why should operator< be non-member function?

吃可爱长大的小学妹 提交于 2020-01-06 08:07:43
问题 I remeber C++ Primer tells us operator< should be non-member function , and I always obey the rule. But now I want to know the reason. I wrote following code: #include <iostream> using std::cout; using std::endl; struct Point1 { int x, y; Point1(const int a, const int b): x(a), y(b) { } }; inline bool operator<(const Point1& lhs, const Point1& rhs) { return lhs.x < rhs.x || (lhs.x == rhs.x && lhs.y < rhs.y); } struct Point2 { int x, y; Point2(const int a, const int b): x(a), y(b) { } bool

Accessing private class in operator<< in namespace

喜夏-厌秋 提交于 2020-01-06 06:28:08
问题 I have a class CFoo with a private inner class CBar. I want to implement a stream ouput operator for CFoo, which in turn uses a stream output for CBar in it's implementation. I can get this working when CFoo is in the common namespace, but when i place it in a new namespace (namespace foobar), the operator can no longer access the private inner class. I suspect this has something to do with the full signature of the operator, but I can't figure out the correct way to specify the friend

Set of custom class objects and their < operator

雨燕双飞 提交于 2020-01-06 06:09:20
问题 I am trying to implement A* algorithm (with visualization in Qt). I've got this method: result_path astar_algorithm::calculate(mapview* m_view) { map_view = m_view; auto closed_set = std::vector<std::shared_ptr<node>>(); auto start_node = std::make_shared<node>(_start); auto open_set = std::vector<std::shared_ptr<node>>{start_node}; std::map<node, node> came_from; std::shared_ptr<node> current; while (!open_set.empty()) { current = *std::min_element(open_set.begin(), open_set.end()); if (

Overloading Extraction operator

删除回忆录丶 提交于 2020-01-06 03:17:25
问题 For a hw assignment for a Time class I have the instructions to overload the extraction operator, however the input format has to be the same as the output which is (days~HH:MM:SS). This is what i have for the operator: header file friend ostream& operator<<(ostream& out, const Time& t); friend istream& operator>>(istream& in, Time& t); cpp file istream& operator>>(istream& in, Time& t) { in >> t.day; in >> t.hour; in >> t.minute; in >> t.second; if (t.day < 0 || t.hour < 0 || t.minute < 0 ||

Implementing access to vector of stucts using a vector as indices

我们两清 提交于 2020-01-06 02:51:06
问题 Some higher-level languages have this feature, and I'm attempting to implement in C++. I'm not sure if there's a library that already does this somewhere, but if there is, it would save me a lot of trouble. Anyway, here's what I'm trying to accomplish: Let's say I have a vector of structs that have a double and an int as members, and that I have another vector of ints that represents the indices of the elements in the vector of structs that I want to keep. typedef struct { double x; int y; }s

Why can't I redefine the : operator in Haskell?

混江龙づ霸主 提交于 2020-01-05 11:56:13
问题 I'm trying to reimplement some functionality of the list data type in Haskell for learning purposes. When I try to redefine : with this code: {-# LANGUAGE NoImplicitPrelude #-} data List a = Nil | Cons a (List a) (:) :: a -> List a -> List a (:) = Cons I get the following error with stack runghc : Invalid type signature: (:) :: ... Should be of form <variable> :: <type> Is it impossible to redefine : ? Is that why I'm getting this error? 回答1: It is impossible to redefine : , but that is not

C# Generic Operators

拈花ヽ惹草 提交于 2020-01-05 06:43:23
问题 I am trying to implement a generic operator like so: class Foo { public static T operator +<T>(T a, T b) { // Do something with a and b that makes sense for operator + here } } Really what I'm trying to do is gracefully handle inheritance. With a standard operator + in Foo, where T is instead "Foo", if anyone is derived from Foo (say Bar inherits Foo), then a Bar + Bar operation will still return a Foo. I was hoping to solve this with a generic operator +, but I just get a syntax error for