Yos
Yos

Reputation: 493

"Cannot convert 'MyClass' to 'bool'" Error in Template Programming

I am playing around with templates and have tried the following but get 'Cannot convert 'MyClass' to 'bool' error?

#include "Unit2.h"
using namespace std;

template <class T>
T GetMax (T a, T b) {
  T result;
  result = (a > b) ? a : b;
  return (result);
}

int main () {
  MyClass k1( 10, "A" );
  MyClass k2( 50, "B" );
  MyClass k3( 0,"" );
  k3 = GetMax<MyClass>(k1,k2);

  cout << k3.GetName() << endl;

  return 0;
}
//---------------------------------------------------------------------------

I have defined a > operator for myclass as follows:

MyClass& MyClass::operator>(MyClass &rhs)
{
    MyClass& rkReturn = ( m_iSize > rhs.m_iSize ) ? *this : rhs;
    return rkReturn; 
}

Upvotes: 1

Views: 1845

Answers (9)

Jason
Jason

Reputation: 32510

The operator> for MyClass should simply define whether one instance of the class is greater than the other, and it should return type bool, not type MyClass&. Modify it so that it looks like the following:

bool MyClass::operator>(const MyClass &rhs) const
{
    return m_iSize > rhs.m_iSize; 
}

This will now properly test whether the current instance of MyClass is larger than the MyClass instance on the right-hand side of the > operator in such an expression.

Upvotes: 0

Mooing Duck
Mooing Duck

Reputation: 66922

The syntax x ? y : z requires x to be convertable to a bool type. You give it the expression (a > b), which calls your operator MyClass& MyClass::operator>(MyClass &rhs), which returns a MyClass by reference. The compiler cannot convert this reference to a bool, and gets confused. MyClass::operator>(MyClass &rhs) should return a bool.

bool MyClass::operator>(MyClass &rhs) const //also, it should be a const function
{
    return m_iSize > rhs.m_iSize 
}

Upvotes: 2

rouzier
rouzier

Reputation: 1180

Try this

bool MyClass::operator>(MyClass &rhs)
{
    return m_iSize > rhs.m_iSize ;
}

Upvotes: 1

Oscar Korz
Oscar Korz

Reputation: 2487

Your operator> returns a MyClass& instead of a bool. By using it in the conditional operator, the compiler is trying to coerce the returned MyClass to a bool.

Change

MyClass& MyClass::operator>(MyClass &rhs)
{
    MyClass& rkReturn = ( m_iSize > rhs.m_iSize ) ? *this : rhs;
    return rkReturn; 
}

to

bool MyClass::operator>(const MyClass &rhs) const
{
    return m_iSize > rhs.m_iSize;
}

Upvotes: 3

Daniel
Daniel

Reputation: 31579

operator > should return bool and not MyClass & like this:

bool MyClass::operator>(MyClass &rhs)
{
    return m_iSize > rhs.m_iSize;
}

Upvotes: 1

Dabbler
Dabbler

Reputation: 9863

operator> should be declared/defined to return bool, not MyClass&.

Upvotes: 1

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361482

You need to return bool from operator>.

So try this:

bool MyClass::operator>(const MyClass &rhs)
{
    return m_iSize > rhs.m_iSize;
}

It would be better if you make this a const function, by putting the keyword on the right side of the function, as shown below:

bool MyClass::operator>(const MyClass &rhs) const 
{                                        // ^^^^^ this makes the function const
    return m_iSize > rhs.m_iSize;
}

Put const on the declaration also.

Upvotes: 1

Amber
Amber

Reputation: 526703

Your > operator needs to return a bool (or something that can be automatically converted to a bool, such as an int), not a MyClass&.

Upvotes: 1

SLaks
SLaks

Reputation: 887469

Your > operator should return a bool, not a MyClass reference.

Upvotes: 6

Related Questions