简答题 C++
题号 #2254670

使用VC++6.0打开考生文件夹下的源程序文件1.cpp,该程序运行时有错误,请改正错误,使得程序输出:
Hello
test
注意:不要改动main函数,不能增加或删除行,也不能更改程序的结构,错误的语句在
//******error******的下面。
试题程序:
#include<iostream>
//********error********
template<T>
voidfun(Tt)
{
std::cout<<"test"<<std::endl;
}
//********error********
template<bool>
voidfun(boolt)
{
std::cout<<(t?"Hell0":"Hi")<<std::endl;
}
intmain()
{
//********error********
boolflag=TRUE;
fun(flag);
fun((int)flag);
return0;
}

进入在线模考

参考答案

暂无

答案解析

(1)应改为“template<classT>”。
(2)删除“template(bool)”。
(3)应改为“boolflag=true;”。
【解析】C++中的模板类格式为template<classT>,所以第1处的语句“template<T>”应修改为“template(classT>”。在第2处的“voidfun(boolt)”函数中,t变量为布尔型变量,是C++中的基础变量,不是模板变量,没有使用到模板类,所以删除第2处的模板类型定义语句。第3处的“boolflag=TRUE;”是声明布尔型变量flag,并将其值赋为逻辑真。而TRUE并不是合法值,C++中区分大小写,所以逻辑真值应为true,即“boolflag=true;”。

你可能感兴趣的试题

找试题 · 找答案 · 找资料 在线模考