How to create comparator for struct | Sololearn: Learn to code for FREE!

+2

How to create comparator for struct

creating comparator related to mock

c++

11/17/2017 6:11:44 AM

Ynelle Kyle Novicio

32 Answers

New Answer

+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.

+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.

+3

Ill implement that as well, but in +, do you want me to add the string (concatenation) or do something else?

+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; }

+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...

+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.

+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.

+3

@blued Just give me some time. Ill check the code in my PC.

+3

It seems you have removed the links to the codes. Please save them again, if possible...

+2

yeah i think it is like some function to compare bet 2 objects or an overloaded == operator....

+2

Can you post the class if possible? Ill then create a == operator. And what all should be the basis of comparison?

+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); };

+2

please help on how could I make comparator for it. it is related to testing of codes.

+2

so comparator is all about overloaded operators?

+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.

+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.

+1

how about for the ComplexStruct operator += and = and for friend ostream?

+1

please explain further.... thanks

+1

@blued I am ready to clear all your doubts. Let me know if you still have a doubt.

+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.