operator-overloading

c++ friend operator template specialization

亡梦爱人 提交于 2021-02-07 09:37:34
问题 I have a generalized modulo struct called quotient_ring . The relevant bits are shown below. template <typename R = long long> struct quotient_ring{ using Q = quotient_ring; R x, m; ... template <typename T> friend constexpr std::basic_ostream<T> &operator<< (std::basic_ostream<T> &str, const Q &q){ return str << '(' << q.x << ")%(" << q.m << ')'; } }; This operator << would print something like 2 mod 7 as (2)%(7) . The reason I need the brackets is because the type R can become very nested.

Overloading operator << - C++

徘徊边缘 提交于 2021-02-06 12:47:26
问题 Background I have a container class which uses vector<std::string> internally. I have provided a method AddChar(std::string) to this wrapper class which does a push_back() to the internal vector. In my code, I have to add multiple items to the container some time. For that I have to use container.AddChar("First"); container.AddChar("Second"); This makes the code larger. So to make it more easier, I plan to overload operator <<. So that I can write container << "First" << "Second" and two

virtual insertion operator overloading for base and derived class

我的未来我决定 提交于 2021-02-05 11:34:07
问题 Can someone please explain how to ensure that the derived function is called from a pointer of type base to a derived object instead of the base function... Also, are the virtual and override keywords best practice to accomplish this? I had previously defined each overload with keyword friend in each class; but the base function was called for the base pointer to derived object. int main() { // contrived example ... base* ptr_derived = new derived(); std::cout << *ptr_derived; delete ptr

Error C4716: 'operator<<': must return a value [closed]

自作多情 提交于 2021-02-05 09:38:29
问题 Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . Improve this question I am struggling to get an appropriate return for this operator (it is not my code, just trying to correct it and I am not as good as I should be in C++ to correct it) can anybody help me with this, it is datatype class defined for high level design of digital

Overloading operator= for double

耗尽温柔 提交于 2021-02-05 05:22:06
问题 Is it possible to overload = operator of type double? I have the following: double operator=(double a, Length b) { return a = (b.getInches()/12+b.getFeet())*3.2808*0.9144; } It throws the following error: 'double operator=(double, Length)' must be a nonstatic member function What am I doing wrong? 回答1: You cannot overload operators for builtin (integral or floating point) types like double , and also you cannot globally overload the = operator for any type. The = operator can only be

About operator overload resolution

自作多情 提交于 2021-02-04 19:09:26
问题 Suppose two classes with the following implicit and explicit operator pattern: class Foo { public static implicit operator decimal (Foo foo) { throw new NotImplementedException(); } public static implicit operator Foo (decimal value) { throw new NotImplementedException(); } public static Foo operator +(Foo left, Foo right) { throw new NotImplementedException(); } } class Bar { public static explicit operator decimal (Bar bar) { throw new NotImplementedException(); } public static explicit

Overloading C++ Insertion Operator (<<)

自闭症网瘾萝莉.ら 提交于 2021-02-04 17:47:43
问题 I'm trying to write a class that overloads the insertion operator but in my header file I get the error. Overloaded 'operator<<' must be a binary operator (has 3 parameters) Here is my code: .h file ostream & operator<<(ostream & os, Domino dom); .cpp file ostream & operator<< (ostream & os, Domino dom) { return os << dom.toString(); } I'm following a text book and this is what they use as an example but its not working for me.. Any suggestions? 回答1: You probably put your operator<< inside a

Why does overloading ostream's operator<< need a reference “&”?

泄露秘密 提交于 2021-02-04 13:49:09
问题 I've been learning C++. From this page, I understood that overloading "<<" operator of ostream can be done in this way. ostream& operator<<(ostream& out, Objects& obj) { // return out; } //Implementation and friend ostream& operator<<(ostream& out, Object& obj); //In the corresponding header file My question is... why does this function need "&" at the end of ostream and Object ? At least I know that "&" is used to... Take the address of a value Declare a reference to a type However, I think

How do I read and parse input from a user that is comma separated by receiving an std::istream object in c++?

天涯浪子 提交于 2021-01-29 10:11:16
问题 I have a class in c++ called Airplane. I need to create a read function using std::istream that lets a user type after a prompt in the console a line that is comma separated. This line of input will then be split up using the commas and assigned to different private data members of the class. As an example, if the user types into the console "abc,12345,hello," then I would need to parse that line and assign abc to one variable, 12345 to another and hello to the last. I believe after the user

Reason for the inability to concatenate strings and ints in Python

允我心安 提交于 2021-01-28 05:30:58
问题 It is well documented in numerous that str is required to convert ints to strings before they can be concatenated: 'I am ' + str(n) + ' years old.' There must be a fundamental reason why Python does not allow 'I am ' + n + ' years old.' and I would like to learn what that reason is. In my project, I print a lot of numbers and end up with code like this. 'The GCD of the numbers ' + str(a) + ', ' + str(b) + ' and ' + str(c) + ' is ' + str(ans) + '.' It would be much prettier if I could drop