Why isn't assert working here? Im getting an error error C2676: binary '==' : 'bigint' does not define thisoperator or a conversion to a type acceptable to the predefined operator
bi is not a built-in data-type, there isn't an implicit conversion of type bigint to type int. For the use of operator==, your bigint class must provide an overloaded member function for operator==. Otherwise cassert would not work, there would always be an error. Moreover, bigint implementation is incomplete.
EDIT: @Daleth, since he's comparing against an int, all he needs is
1 2 3
inlinebooloperator==(constint &rhs)
{
}
Then, there isn't a data member to compare the object against, is there?
You can define booloperator==(const bigint&, const bigint&), and the 0 will be used to create an rvalue bigint object. It beats having to define every single overload for all situations. (The overhead from creating rvalue objects would probably be negligible).
I'm not sure what you meant with your last statement. All ADTR would have to do is return the result of compare(). (ADTR doesn't really need the comp variable, but ADTR should at least initialize it to false).
inlinebooloperator==(const big &lhs, const bigint &rhs);
Afaik would require two distinct arguments of type bigint(or type implicitly convertible to bigint or a temporary constructed with any of the bigint ctors), this is already included. All you have to compare is the object in execution(in this case, bi) and the rhs(which might be an int or any bigint object).
This is my point operator== don't need two parameters, one of it would be a waste. At least all you have to do is compare TWO, I repeat TWO, elements. operator this is already captured, as long as operator== is a member function of class bigint, all he needs in the Value or class object to compare it with.
Compiler speaking, the generated code would look like this:
1 2 3 4 5 6
booloperator==(const bigint constthis, const bigint &rhs)
{
/** now in here, the comparison takes place
where elements/members of operator this-> and elements of rhs to be compared is/are executed
**/
}
Oddly speaking, even if you try it with 3(or more) bigint objects. It would only compare 2 objects first and use the value for all other elements.
A call to assert(bi==_bi==__bi==5)
it would still work
string s = "Hello, world";
if(s == "Hello, world") //the hello world here is technically a char*
//but is passed as string("Hello, world")
//ie its s == string("Hello, world")
//...
Call bi==_bi==__bi==5 , it would still work
thats chaining. its because everything is resolved to true/false so its saying
bi == ( _bi == (__bi == 5))
@Matri X
You are describing a member function operator==:
bool bigint::operator==(const bigint&);
I was talking about a non-member:
booloperator==(const bigint&, const bigint&);
You should be able to tell the difference because: 1) I do not scope the class (which you should have done actually, to avoid confusion); 2) I put in two parameters, and all operators have a fixed number of parameters.
operator== doesn't need to be a member function. As I said before, all the OP has to do is return the result from compare:
You probably noticed that I wrote a pass by value function instead of const reference. This is because the OP's current compare function is not read-only.
I am still confused and can't get this to work. (Sorry I am very new to c++ and have never dealt with operator overloading) So should I have bool operator == as a member function? And do I have 1 or 2 parameters? I need some clarification please.
Overloading an operator either globally or locally makes no ounce of difference except that the declaration is different. Though, since you're new, I wouldn't worry too much about where you define your operator, so long as it's in a valid place.
If you define an operator globally, you'd write something like this:
1 2 3 4 5 6 7
class SomeClass;
// Globally:
booloperator == ( SomeClass A, SomeClass B )
{
// Compare "A" to "B".
}
If you define an operator as a member-function, you'd write:
1 2 3 4 5 6 7 8 9 10 11 12
class SomeClass
{
private:
int A;
public:
booloperator == ( SomeClass Other ) const // "const" is optional, though preferred
{
// Compare "this" to "Other".
return( A == Other.A );
}
};
Why are you using assertions in the first place? Its use is not warranted here. If you insist on assertions, fine. Note that an assertion will fail is the conditional it false.
In your equality operator, you're comparing the addresses of the "value" array of both left-hand and right-hand operands. Of course, the assertion will fail because the addresses will be different. Also, your assertion condition makes no sense.
What you need to do is change the condition of the assertion:
assert( bi != 0 );
In addition, I would have a member-function that performs some kind of validation.