i'm trying overload the != and == operators, but i'm getting several errors :(

bool operator==(const image &other) const
    {
        if(other.btBitmap==*this->btBitmap)
            return true;
        else
            return false;
    }

    bool operator!=(const image &other) const
    {
        return !(*this == other);
    }

what i'm doing wrong?

You have a superfluous * character on line 3. This should work:

bool operator==(const image &other) const
{
    return (other.btBitmap == this->btBitmap)
}
bool operator!=(const image &other) const
{
    return !(*this == other);
}
Comments
thanks for all

i continue with errors, when i do:

if(inst->imgtest!=NULL)

i get errors:
- "ambiguous overload for 'operator!=' (operand types are 'image' and 'int')"

Moschops: image.... my own class. see mike post ;)
i'm failling with these operator.
inst is the control object pointer. so the important thing is the imgtest image object.

Is imgtest a pointer? I am guessing it is not because of the error :

"ambiguous overload for 'operator!=' (operand types are 'image' and 'int')"

If a type is not a pointer you should not try to comapre it to NULL. NULL is just #define NULL 0 so anywhere you have null it just gets substituted with 0. With C++ 11 you should use nullptr to check if a pointer is null. If you are trying to check that imgtest is valid then I would suggest you write a function in you class that will check the validity of the object and return true or false. Then you cold write somethin like:

if(inst->imgtest.isValid())
Comments
thanks for all

if i do these works fine:

if((HBITMAP)inst->imgtest!=NULL)

but i want avoid the casting...

Moschops: image.... my own class. see mike post ;)

I read Mike's post but he doesn't say anything about what kind of object imgTest is.

What are you actually trying to test? If imgtest is not a pointer, testing if it is a null pointer makes no sense.

Edited 1 Year Ago by Moschops

my objective is testing if the imgtest.btBitmap(is the image HBITMAP) have any image with != operator, just using my image object name.

Well if the default construction of an image leaves the image in an invalid state you could use:

if(inst->imgtest != image())

this will test imgtest against an default object. As I said earlier NULL only makes sense to test for a valid pointer but you should use nullptr if you can. I don't see why you wouldn't just write an isValid() function.

Edited 1 Year Ago by NathanOliver

now it's fixed:

bool operator != ( nullptr_t ) const
{
    return btBitmap != nullptr;
}

i must add these function to my class image and now works fine.
thanks to all
but continue: i have several problems with some overload operators(even reading all tutorials) :(
so what can you advice me?

I would advise you that what you've done there with the != operator is very counter-intuitive. The != operator is usually used to test if object1 is the same as object2.

You're using it to test if object1 is valid. There is no object2.

Overloading operators is meant to make your code easier to understand. Using the != like this makes it harder to understand.

Comments
honestly i agree with you

i think:

    if(inst->imgtest != NULL)

more easy ;)
because test if imgtest have 1 image or not, of course that i can use haveimage() function ;)

bool haveimage()
    {
        if(btBitmap==NULL)
            return false;
        else
            return true;
    }

//testing
if(inst->imgtest.haveimage()==true)

Edited 1 Year Ago by cambalinho

You wouldn't have to write

if(inst->imgtest.haveimage()==true)

That could be expressed as

if(inst->imgtest.haveimage())

When using an IDE that has auto-complete it is even faster to write.

I also want to stress that if you are using a C++11 compliant compiler to stop using NULL and start using nullptr. Now that we have nullptr there is no reason to ever use NULL.

Comments
thanks for that

honestly that types of if's are confused to me:

if(inst->imgtest.haveimage())//same: if(inst->imgtest.haveimage()==true)

//and

if(!inst->imgtest.haveimage())//same: if(inst->imgtest.haveimage()==false)

//correct me if i'm wrong ;)

yes i use C++ 11 type with compiler... my all class's are prepared with some C++ 11 code ;)

haveimage() returns true or false so if haveimage() returns true then if(inst->imgtest.haveimage()) becomes if(true) and the body of the if statement is executed. If it were false then if(inst->imgtest.haveimage()) becomes if(false) and the body of the if statement is not executed.

let me ask: why, sometimes, we use '*' and othertimes don't with 'this'?

I assume you are talking about return types when asking this question. this is a pointer to the object that the function belongs to. If the function is returning itself as an object like SomeObject foo() then you use
return *this so that you return a SomeObject and not a SomeObject* since * dereferences the pointer. If you had SomeObject* foo() then you would use return this

Comments
thanks for all
This question has already been answered. Start a new discussion instead.