我对面向对象的代码比较新,所以我可能会遗漏一些简单的东西。下面的代码主要是来自另一个文件中的函数原型的骨架代码。问题出在Light :: setMessage()函数中。因为我编译时遇到了这个错误。
metaphase.cpp:25:27: error: cannot convert ‘Light::checkMessageValid’ from type ‘bool (Light::)()’ to type ‘bool’
由于我是班级的新手,如果我做错了什么就不会让我感到惊讶,但是如果我删除了if(checkMessageValid)而将其他人编译掉了。我知道这些函数中没有任何东西,但我不明白为什么编译器不会完全相同。
#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;
}
答案 0 :(得分:1)
正如你所说,checkMessageValid
是一个函数,而不是一个变量。必须调用函数checkMessageValid()
。
该错误基本上表明checkMessageValid
(类型bool (Light::)()
)无法转换为bool
。 if
语句需要bool
(进行比较),因此编译器会尝试将函数类型转换为bool
,但会失败,因为没有这样的转换。