operator-overloading

Getting “Use of undefined type” and “Must have class/struct/union” errors

不羁的心 提交于 2019-12-31 03:53:08
问题 EDIT #1: Everything before editing the line << "Owner: " << (*wo._owner).getLoginName() << endl; worked completely fine, or at least didn't throw errors on me. So I have the following code (obviously there is a lot more that if requested I will post, just not sure if more is needed or that is ok): class Workout { private: int _workoutid; // the ID of this workout User* _owner; // Who did this workout float _distance; // in miles int _duration; // in seconds int _day, _month, _year; // date:

Why overloaded ' operator < ' should be const for class?

我是研究僧i 提交于 2019-12-31 02:15:14
问题 Can anybody explain this behavior in context of STL sort algorithm? If operator < is not defined const it gives error, error: passing ‘const B’ as ‘this’ argument of ‘bool B::operator<(const B&)’ discards qualifiers [-fpermissive] while (__pivot < *__last) Is sort algo lhs const object or sort is const method? class B { public: ... bool operator < (const B& b) const // why const required here? { return (m_i < b.m_i); } ... private: int m_i; int m_j; }; int main() { vector<B> Bvec2 {B(5), B(3)

Overload output stream operator of a template class outside of the template

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-31 01:53:10
问题 I want to overload the output stream operator << outside the template class definition. Implementing it inside the template class is ok: template <typename T,int _MaxSize=10,template <class C> class Policy=NoCheck,typename Container=std::vector<T>> class MyContainer : public Policy<T> { public: MyContainer():p(_MaxSize){}; std::ostream& operator<<(MyContainer<T,_MaxSize,Policy,Container>& obj){ }; private: Container p; }; But when I tried to do it outside the template class: template

What is the __lt__ actually doing for lists [duplicate]

房东的猫 提交于 2019-12-30 22:58:31
问题 This question already has answers here : Comparing two lists using the greater than or less than operator (2 answers) Closed 3 years ago . Say I have two lists, and I run the following command >>> s = [1, 2, 3] >>> t = [1, 2, 4] >>> s > t False >>> s < t True But if I were to run the following command >>> s = [1, 2, 3] >>> t = [1, 1, 4] >>> s > t True >>> s < t False Have to admit, I'm not too familiar with the PY3 codebase. What exactly is going on in the __lt__, __le__, __gt__, __ge__, __ne

Introduction To C++ IO Streams

强颜欢笑 提交于 2019-12-30 19:18:06
问题 I got a snippet of code from this article and I'm confused as to how it works? The snippet starts by saying: You can detect that a particular read or write operation failed by testing the result of the read. For example, to check that a valid integer is read from the user, you can do this: int x; if ( cin >> x ) { cout << "Please enter a valid number" << endl; } This works because the read operation returns a reference to the stream. I understand that the cin >> x operation returns a

Introduction To C++ IO Streams

淺唱寂寞╮ 提交于 2019-12-30 19:16:51
问题 I got a snippet of code from this article and I'm confused as to how it works? The snippet starts by saying: You can detect that a particular read or write operation failed by testing the result of the read. For example, to check that a valid integer is read from the user, you can do this: int x; if ( cin >> x ) { cout << "Please enter a valid number" << endl; } This works because the read operation returns a reference to the stream. I understand that the cin >> x operation returns a

Infix vs prefix syntax: name lookup differences

拟墨画扇 提交于 2019-12-30 17:21:51
问题 Operators in C++ are usually considered to be an alternative syntax for functions/methods, especially in the context of overloading. If so, the two expressions below should be synonymous: std::cout << 42; operator<<(std::cout, 42); In practise, the second statement leads to the following error: call of overloaded ‘operator<<(std::ostream&, int)’ is ambiguous As usual, such error message is accompanied with a list of possible candidates, these are: operator<<(basic_ostream<_CharT, _Traits>& _

C++ overloaded operator with reverse order of associativity

主宰稳场 提交于 2019-12-30 09:19:47
问题 It was very hard to come up with a title... (I'm not a native English speaker.) struct A { int value; A operator+(int i) const { A a; a.value=value+i; return a; }; }; int main(){ A a; a.value=2; a=a+2; return 0; } This code compiles/works as expected, but when I change a=a+2 to a=2+a, it won't compile anymore. GCC gives me this error: no match for ”operator+” in ”2 + a” Is there any way to somehow make 2+a work just like a+2? 回答1: You need a free function, defined after the class struct A { /

Can refactoring an overloaded operator into a non-member function break any code?

こ雲淡風輕ζ 提交于 2019-12-30 08:24:24
问题 Consider a legacy class template with overloaded addition operators += and + template<class T> class X { public: X() = default; /* implicict */ X(T v): val(v) {} X<T>& operator+=(X<T> const& rhs) { val += rhs.val; return *this; } X<T> operator+ (X<T> const& rhs) const { return X<T>(*this) += rhs; } private: T val; }; Upon code review, it is observed that + is implementable in terms of += , so why not make it a non-member (and have guaranteed symmetry for left and right arguments)? template

casting operator - const vs non-const

不羁的心 提交于 2019-12-30 08:05:13
问题 I have this code sample: class Number { int i; public: Number(int i1): i(i1) {} operator int() const {return i;} }; What are the implications of removing the const modifier from the casting operator? Does it affect auto casting, and why? 回答1: If the conversion operator is not const, you can't convert const objects: const Number n(5); int x = n; // error: cannot call non-const conversion operator 回答2: If you have a function like this: void f(const Number& n) { int n1 = n; } It will start