C Assert

Why isn't assert working here? Im getting an error error C2676: binary '==' : 'bigint' does not define this operator or a conversion to a type acceptable to the predefined operator


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef BIGINT_H
#define BIGINT_H

const int MAXINT = 500;

class bigint{
	public:
		bigint();
		bigint(int);
		bigint(char[]);
		void output (std::ostream& out);
		bool compare(const bigint &rhs);

	private:
		int value[MAXINT];
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include "bigint.h"
#include <cassert>

int main(){
	// Test 
    bigint bi;

    // Verify
    assert(bi == 0);
    std::cout << "0 == ";
    bi.output(std::cout);
    std::cout << std::endl;

    std::cout << "Done testing default constructor." << std::endl;
}
bigint::bigint(){
	
	for(int i=0; i <= MAXINT; i++)
		value[i] = 0;
}

bigint::bigint(int digit){

	int numdigits = log10((double)digit) + 1;

	for (int i=numdigits-1; i >= 0; i--)
	{
		value[i] = digit%10;
		digit /= 10;
	}
}

bigint::bigint(char *number){
	
	for (int i=0; i <= MAXINT; i++){
		number[i] =- '0';
		number[i] = value[i];
	}
}

void bigint::output(std::ostream& o){

	for(int i=0; i <= MAXINT; i++)
		std::cout << value [i];
}

bool bigint::compare(const bigint &rhs){

	bool comp;

	for(int i=0; i <= MAXINT; i++){
		if(value[i] == rhs.value[i])
			comp = true;
	}
	return comp;
}
Exactly what it means. You defined no bool operator==(const bigint&, const bigint&) function.
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
inline bool operator==(const int &rhs)
{
}

Then, there isn't a data member to compare the object against, is there?
Last edited on
You can define bool operator==(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).
Last edited on
The last statement wasn't for you.
 
inline bool operator==(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).
He provides a constructor for an int. So the 0 in bi == 0 will be used to construct an rvalue bigint for that function. It is perfectly fine.
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
bool operator==(const bigint const this, 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
Last edited on
closed account (Dy7SLyTq)
its like this matrix:
1
2
3
4
5
6
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))
I omitted the parenthesis, my bad
@Matri X
You are describing a member function operator==:
 
bool bigint::operator==(const bigint&);


I was talking about a non-member:
 
bool operator==(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:
1
2
inline bool operator==(bigint lhs, bigint rhs)
   {return lhs.compare(rhs);}

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.
At least now we have an understanding
closed account (zb0S216C)
I should point out that "assert( )" should only appear in debug mode. A program's user should not experience an assertion failure.

1
2
3
4
5
6
7
8
int main( int Argc, char **Argv )
{
  #if( defined NDEBUG )
    assert( Argv != NULL );
  #endif

  // ...
}

Wazzak
Last edited on
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.
closed account (zb0S216C)
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:
bool operator == ( 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:
    bool operator == ( SomeClass Other ) const // "const" is optional, though preferred
    {
      // Compare "this" to "Other".
      return( A == Other.A );
    }
};

Wazzak
Last edited on
Okay thank you so much for clarifying. My program currently crashes when i compile and run it and I'm not sure why.

Header File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef BIGINT_H
#define BIGINT_H

const int MAXINT = 500;

class bigint{
	public:
		bigint();
		bigint(int);
		bigint(const char *value);
		void output (void);
		bool compare(const bigint &rhs);
		bool operator == (bigint rhs)const{return(value == rhs.value);}
		
	private:
		int value[MAXINT];
};

#endif 


Part where I am trying to assert

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main ()
{
    // Setup

    // Test 
    bigint bi;

    // Verify
    assert(bi == 0);
    std::cout << "0 == ";
    bi.output();
    std::cout << std::endl;

    std::cout << "Done testing default constructor." << std::endl;
}


I have a default constructor so it is initialized to 0

1
2
3
4
5
bigint::bigint(){
	
	for(int i=0; i <= MAXINT; i++)
		value[i] = 0;
}


And I also have a compare method that maybe I should be using somehow?

1
2
3
4
5
6
7
8
9
10
11
12
bool bigint::compare(const bigint &rhs){

	bool comp;

	for(int i=0; i <= MAXINT; i++){
		if(value[i] == rhs.value[i])
			comp = true;
		else
			return false;
	}
	return comp;
}
closed account (zb0S216C)
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.

1
2
3
4
5
6
7
8
9
class bigint
{
  public:
    // ...
    void Validate( )
    {
      assert( /* ...test... */ );
    }
};

Wazzak
Last edited on
@Framework Thank you I figured out what needed to be fixed
Topic archived. No new replies allowed.