operator-overloading

Compare two objects with “<” or “>” operators in Java

蓝咒 提交于 2019-12-28 03:05:42
问题 How to make two objects in Java comparable using "<" or ">" e.g. MyObject<String> obj1= new MyObject<String>(“blablabla”, 25); MyObject<String> obj2= new MyObject<String>(“nannaanana”, 17); if (obj1 > obj2) do something. I've made MyObject class header as public class MyObject<T extends Comparable<T>> implements Comparable<MyObject<T>> and created method Comp but all the gain I got is now I can use "sort" on the list of objects, but how can I compare two objects to each other directly? Is if

no viable conversion from 'int' to 'Time'

﹥>﹥吖頭↗ 提交于 2019-12-25 19:09:10
问题 I am having an error in a user defined + operator: #include <iostream> using namespace std; class Time{ int h; int m; int s; public: Time(); Time(int x,int y, int z) { h=x; m=y; s=z; } operator int() { return(h*3600 + m*60 + s); } void display() { cout<<h<<endl; cout<<m<<endl; cout<<s<<endl; } }; int main(){ Time t1(2, 3, 4); Time t2 = 200 + (int)t1; t2.display(); } note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const Time &' for 1st

Operator Overloading: How to add and compare data of two class “Integer” and “Fraction”

天涯浪子 提交于 2019-12-25 18:35:29
问题 I have two classes Integer and Fraction and one abstract class Number . I am suppose to perform addtion operation by overloading + and also I need to check equality of values using overloading of == operator on these classes. Operations to be performed 1. Add Integer + Integer = Integer 2. Add Fraction + Fraction = Fraction 3. Add Integer + Fraction = Fraction I have been able to do 1st and 2nd operation but not able to do addition of integer and fraction. Below is the code snippet: Number.h

Trying to reload an operator in c++, but it doesn't seem to work

强颜欢笑 提交于 2019-12-25 18:30:18
问题 Problem: I have overloaded operators * and *= with the same solution, though using operator *= doesn't seem to change the contents of the Matrix, maybe I am declaring the operator overload method incorrectly. At the same time, operator * works properly and actually multiplies Matrix, I have checked it beforehand. Output: 3 4 -5 8 0 7 8 9 -4 8 7 7 -6 0 6 2 2 9 3 4 -5 8 0 7 8 9 -4 Here is the code itself: struct WrappedMatrix{ int n; int ** Matrix; }; struct WrappedVector{ int n; int * Vector;

overload of operator: ok MSVS but fails in g++

廉价感情. 提交于 2019-12-25 11:28:07
问题 I have code which compiles without error in Visual Studio 2010. But g++ puts error CComplex.cpp: In member function 'Complex Complex::operator+(Complex&)': CComplex.cpp:22: error: no matching function for call to 'Complex::Complex(Complex)' CComplex.cpp:15: note: candidates are: Complex::Complex(Complex&) make: *** [CComplex.o] Error 1 Please tell me what's the problem with my code. Complex.h class Complex { public: Complex(); Complex(double _Re, double _Im); Complex(Complex& c); Complex

overload of operator: ok MSVS but fails in g++

不羁的心 提交于 2019-12-25 11:27:36
问题 I have code which compiles without error in Visual Studio 2010. But g++ puts error CComplex.cpp: In member function 'Complex Complex::operator+(Complex&)': CComplex.cpp:22: error: no matching function for call to 'Complex::Complex(Complex)' CComplex.cpp:15: note: candidates are: Complex::Complex(Complex&) make: *** [CComplex.o] Error 1 Please tell me what's the problem with my code. Complex.h class Complex { public: Complex(); Complex(double _Re, double _Im); Complex(Complex& c); Complex

overload of operator: ok MSVS but fails in g++

ⅰ亾dé卋堺 提交于 2019-12-25 11:27:35
问题 I have code which compiles without error in Visual Studio 2010. But g++ puts error CComplex.cpp: In member function 'Complex Complex::operator+(Complex&)': CComplex.cpp:22: error: no matching function for call to 'Complex::Complex(Complex)' CComplex.cpp:15: note: candidates are: Complex::Complex(Complex&) make: *** [CComplex.o] Error 1 Please tell me what's the problem with my code. Complex.h class Complex { public: Complex(); Complex(double _Re, double _Im); Complex(Complex& c); Complex

C++ return by value - what happens with the pointer inside?

不羁岁月 提交于 2019-12-25 05:32:10
问题 So I'm having a bit of SegFaults and was wondering if someone can explain me in higher depth how does this work. I have a wrapper class around a numeric variable, and I'm trying to build a compute tree of the expression. Here is a sample code: class DoubleWrap{ public: static int id; DoubleWrap(double value){this->value = value;myid=++id;} double value; int myid; std::vector<DoubleWrap*> parents; DoubleWrap operator+(DoubleWrap& x){ DoubleWrap result(this->value + x.value); setParents(*this

C++ - Linked List code - LNode was not declared in this scope

若如初见. 提交于 2019-12-25 04:37:11
问题 I am writing a class for a linked list in C++ and I am having an issue when writing the operator overload for <<. My class' header is: class LList { private: struct LNode { LNode (); int data; LNode * next; }; public: LList (); LList (const LList & other); ~LList (); LList & operator = (const LList & other); bool operator == (const LList & other); int Size () const; friend ostream & operator << (ostream & outs, const LList & L); bool InsertFirst (const int & value); bool InsertLast (const int

Error: initial value of reference to non-const must be an value

陌路散爱 提交于 2019-12-25 03:49:27
问题 class A { public: int v; A * p; A& operator*(const A& a) { return this->v*a.v// here is a red line under this say error initial value of reference to non-const must be an value } ~A() { this; } }; int main() { A a; a.p = new A; delete a.p; return 0; system("pause"); return 0; } overloading * operator I cannot use this to represent the object itself. Why this happened. 回答1: Surely it says that it must be an lvalue . You're trying to return a reference to a temporary. This is bad karma. Besides