简体   繁体   中英

strange type conversion with one particular function

I'm relatively new to object oriented code so I may be missing something simple. The code below is mostly skeleton code from function prototypes in another file. The problem is with the Light::setMessage() function. As is, I'm getting this error when I compile.

metaphase.cpp:25:27: error: cannot convert ‘Light::checkMessageValid’ from type ‘bool (Light::)()’ to type ‘bool’

Since I'm new to classes, it wouldn't suprise me if I'm doing something wrong, but if I remove the if(checkMessageValid) and leave the others it compiles. I know there is nothing in these functions but I don't see why the compiler isn't treating them exactly the same.

#include "lighting.hpp"

bool Light::checkMessageValid( void )
{
    return false;
}

bool Light::_setChannel( unsigned int ch )
{
    return false;
}

bool Light::_setCommand( unsigned int co )
{
    return false;
}

bool Light::_setData( unsigned int d )
{
    return false;
}

bool Light::setMessage( unsigned int ch, unsigned int co, unsigned int d )
{
    if( checkMessageValid )
    {
        if( !_setChannel( ch ) )
            return false;
        if( !_setCommand( co ) )
            return false;
        if( !_setData( d ) )
            return false;
    }
    return false;
}

As you said, checkMessageValid is a function, not a variable. Functions have to get called, checkMessageValid() .

The error basically states that checkMessageValid (of type bool (Light::)() ) can't be converted into a bool . An if statement requires a bool (to compare), and so the compiler tries to convert the function type into a bool , but fails, because there is no such conversion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM