问答题
使用VC6打开考生文件夹下的源程序文件modi1.clap,该程序运行时有错误,请改正错误,使得程序输出: Hello test 注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//******error******的下面。#include<iostream>//********error********template<T>void fun(T t){ std::cout<<''test''<<std::endl;}//********error********template<bool>void fun(bool t){ std::cout<<(t?''Hello'':''Hi'')<<std::endl;}int main(){ //********error******** bool flag=TRUE; fun(flag);fun((int)flag); return 0;}
【正确答案】正确答案:(1)template<classT> (2)删除template<bool> (3)bool nag=true;
【答案解析】解析:(1)C++中的模板类格式为template<class T>,所以将题目中程序中的第1个标识下语句“template<T>”修改为“template<class T>”。 (2)在第2个标识下的“void fun(bool t)”函数中,其中t变量为布尔型变量,是C++中的基础变量,并不是模板变量,并没有使用到模板类,所以删除第2个标识下的模板类型定义语句。 (3)第3个标识下的“bool flag=TRUE;”是声明布尔型变量flag,并将其值赋为逻辑真。而TRUE并不是合法值,C++中区分大小写,所以逻辑真值应为tnle,即“bool flag=true;”。