+ 2

How to create comparator for struct

creating comparator related to mock

17th Nov 2017, 6:11 AM
Ynelle Kyle Novicio
Ynelle Kyle Novicio - avatar
32 Respuestas
+ 4
You may add this in the class, after removing the prototype: bool operator ==(ComplexStruct b) { return (mInt == b.mInt && mString == b.mString && mDouble == b.mDouble && mBool == b.mBool); } There is no need to pass a constant reference of the object to this operator. Just pass the object itself.
20th Nov 2017, 6:41 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Comparator? Like some function to compare between two objects or an overloaded == operator? Or is it something else? Please try elaborating your query a little.
17th Nov 2017, 12:48 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Ill implement that as well, but in +, do you want me to add the string (concatenation) or do something else?
20th Nov 2017, 6:51 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Here: friend ostream& operator<<(ostream& os, const ComplexStruct& c) { os<<c.mInt<<" "<<c.mDouble<<" "<<boolalpha<<c.mBool<<" "<<c.mString; return os; } ComplexStruct operator + (const ComplexStruct& c) { ComplexStruct res (mInt+c.mInt, mDouble+c.mDouble, mBool||c.mBool, mString+c.mString); return res; } ComplexStruct& operator += (const ComplexStruct& c) { *this = *this + c; return *this; }
20th Nov 2017, 7:01 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
@blued A comparator is just the overloaded form of one of the following operators : ==, !=, <, >, <=, >=. These are also called relational operators, as they check/compare two objects for a relation...
20th Nov 2017, 7:03 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
@blued If you need to place the class in a header, you may keep your prototypes, but now in a seperate cpp file named just the same as the header, you paste the operator overloads. But remember that you now need to add a ComplexStruct:: Scope before all the operators like this : bool ComplexStruct:: operator==(const ComplexStruct& b) { //The code posted earlier. } And for friend operators, you don't need to add this scope part, but instead even remove the friend word from the name.
20th Nov 2017, 1:15 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
@blued In a seperate file? You just need to do exactly what you did for the other operators in sut.cpp. Just add the code like the other operator overloads there.
22nd Nov 2017, 5:26 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
@blued Just give me some time. Ill check the code in my PC.
23rd Nov 2017, 3:56 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
It seems you have removed the links to the codes. Please save them again, if possible...
23rd Nov 2017, 5:49 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
yeah i think it is like some function to compare bet 2 objects or an overloaded == operator....
20th Nov 2017, 2:54 AM
Ynelle Kyle Novicio
Ynelle Kyle Novicio - avatar
+ 2
Can you post the class if possible? Ill then create a == operator. And what all should be the basis of comparison?
20th Nov 2017, 3:04 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
here is the code.... // in header file struct ComplexStruct { int mInt = 0; double mDouble = 0.0; bool mBool = false; std::string mString; ComplexStruct operator+=(const ComplexStruct& cs); ComplexStruct operator+(const ComplexStruct& cs); bool operator==(const ComplexStruct& cs); friend std::ostream& operator<<(std::ostream& os, const ComplexStruct& cs); };
20th Nov 2017, 6:04 AM
Ynelle Kyle Novicio
Ynelle Kyle Novicio - avatar
+ 2
please help on how could I make comparator for it. it is related to testing of codes.
20th Nov 2017, 6:05 AM
Ynelle Kyle Novicio
Ynelle Kyle Novicio - avatar
+ 2
so comparator is all about overloaded operators?
20th Nov 2017, 7:00 AM
Ynelle Kyle Novicio
Ynelle Kyle Novicio - avatar
+ 2
actually i am not confident in my programming skill. but i really want to understand how to have comparator for it. and thanks to both of you for your tips and comments.
20th Nov 2017, 9:05 AM
Ynelle Kyle Novicio
Ynelle Kyle Novicio - avatar
+ 2
@Gordie Sorry. I just don't see any use of doing: ComplexStruct a,b,c; (a+=b)=c; But if I had continued using the object copy returning version, a would never be c. But still, I'll add that as it is a better practice to do so.
20th Nov 2017, 10:01 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
how about for the ComplexStruct operator += and = and for friend ostream?
20th Nov 2017, 6:47 AM
Ynelle Kyle Novicio
Ynelle Kyle Novicio - avatar
+ 1
please explain further.... thanks
20th Nov 2017, 8:34 AM
Ynelle Kyle Novicio
Ynelle Kyle Novicio - avatar
+ 1
@blued I am ready to clear all your doubts. Let me know if you still have a doubt.
20th Nov 2017, 9:58 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
Actually, I am confuse if where I should base the creation of comparator. Is it in the class in the header file or in the definition of function file or source file? because the use of operators are also indicated in the source file.
20th Nov 2017, 10:22 AM
Ynelle Kyle Novicio
Ynelle Kyle Novicio - avatar