如何使用布尔作为函数的返回类型?尝试了一个简单的程序,但完全不知道如何处理... [英] how to use bool as return type of function? Tried a simple program But Have no idea at all how to deal with it...

查看:481
本文介绍了如何使用布尔作为函数的返回类型?尝试了一个简单的程序,但完全不知道如何处理...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream.h>
#include<stdbool.h>  //error in including header file
#include<conio.h>

bool cmp(int,int);  //Declaration error

void main()
{

int a=0,b=1;

cmp(a,b);

getch();

}

bool cmp(int a,int b)  //bool is intialized more than once
{

if(a==b)
return true;
else
return false;

}

推荐答案

代码段中没有语法错误.显然,问题出在编译器上. 此处据说较早的编译器可能不支持<stdbool.h></stdbool.h>.

还有Turbo C ++?真的吗 ?您应该立即安装一个新的C ++编译器(尝试; CodeBlocks,VC ++ ...). 这是不使用Turbo C ++的原因.
There''re is no syntax error in your code snippet. Obviously, the problem is with the compiler. Here it is said that older compilers may not support <stdbool.h></stdbool.h>.

And Turbo C++ ? Really ? You should install a new C++ compiler immediately (Try; CodeBlocks, VC++...). Here''re the reasons not to use Turbo C++.


您对cmp的调用将丢弃结果,该结果应为:
Your call to cmp throws away the result, it should be:
void main()
{
    int a=0;
    int b=1;
    bool result;
     
    result = cmp(a,b);
    if (result)
    {
        // the result is TRUE, do something useful
    }
    else
    {
        // the result is FALSE, do something else
    }
         
    getch();
     
}


这篇关于如何使用布尔作为函数的返回类型?尝试了一个简单的程序,但完全不知道如何处理...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆