在使用ar<<和ar>>对数据进行输入输出时,CArchive::operator >> 对bool型没重载, 必须换成BOOL 。我在一个程序里曾碰到这样的问题,声明一个bool型数组,然后想将其信息输入:
bool m_bColor[64];
...
for(i=0; i<64; i++)
{
ar<<m_bColor[i]; //信息输入
}
但编译后出现如下错误:error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'bool' (or there is no acceptable conversion)。
将bool换成BOOL问题得以解决。
那么,bool和BOOL到底有什么区别呢?在查MSDN时CArchive::operator <<时:
CArchive::operator <<
This operator stores the indicated object or primitive type to the archive.
friend CArchive& operator <<( CArchive& ar, const CObject* pOb );
throw( CArchiveException, CFileException );
CArchive& operator <<( BYTE by );
throw( CArchiveException, CFileException );
CArchive& operator <<( WORD w );
throw( CArchiveException, CFileException );
CArchive& operator <<( LONG l );
throw( CArchiveException, CFileException );
CArchive& operator <<( DWORD dw );
throw( CArchiveException, CFileException );
CArchive& operator <<( float f );
throw( CArchiveException, CFileException );
CArchive& operator <<( double d );
throw( CArchiveException, CFileException );
发现并没有单独对BOOL和bool类型变量重载。事实上,BOOL就是int 型。
bool在C++里是占用1字节,而BOOL是int类型,int类型的大小是视具体环境而定的;所以来说:false/true只占用1个字节,而TRUE/FALSE视具体环境而言,以下是BOOL在windef.h中的定义:typedef int BOOL;
false/true是标准C++语言里新增的关键字,而FALSE/TRUE是通过#define,这要用途是解决程序在C与C++中环境的差异,以下是FALSE/TRUE在windef.h的定义:
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
也就是说FALSE/TRUE是int类型,而false/true是bool类型;所以两者不一样的,只不过我们在使用中没有这种感觉,因为C++会帮你做隐式转换。