c-g如何不报告给定代码的错误?
作者:互联网
这是我对why is elapsedtime giving me an output of 1?的回答的继续
我能够使用g 4.7.3成功编译并构建以下程序.
#include <iostream>
using namespace std;
int elapsedtime(int time1, int time2)
{
return (time2-time1);
}
int main()
{
int time1;
int time2;
cin >> time1 >> time2;
cout << "Your elapsed time is " << elapsedtime <<endl;
}
main中最后一行的意图是:
cout << "Your elapsed time is " << elapsedtime(time1, time2) <<endl;
g如何能够无错误地编译第一个版本?
解决方法:
std :: ostream具有运算符<< (布尔),并且在标准下,函数名称可以隐式转换为布尔(通过函数到指针的转换以及布尔转换).相关语言为(§4[conv] /p1,§4.3 [conv.func],§4.12[conv.bool]):
A standard conversion sequence is a sequence of standard conversions
in the following order:
- Zero or one conversion from the following set: lvalue-to-rvalue conversion, array-to-pointer conversion, and function-to-pointer conversion.
- Zero or one conversion from the following set: integral promotions, floating point promotion, integral conversions, floating point conversions, floating-integral conversions, pointer conversions, pointer to member conversions, and boolean conversions.
- Zero or one qualification conversion.
An lvalue of function type T can be converted to a prvalue of type
“pointer to T.” The result is a pointer to the function.A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to
member type can be converted to a prvalue of typebool
. A zero
value, null pointer value, or null member pointer value is converted
tofalse
; any other value is converted totrue
.