【发布时间】:2016-02-02 13:29:42
【问题描述】:
我使用了#include <typeinfo>头文件下的typeid(var).name()函数,我看到它只返回一个字符。
例如。
#include <iostream>
#include <typeinfo>
int main()
{
std::cout << typeid(5).name() << std::endl;
std::cout << typeid(5.8).name() << std::endl;
return 0;
}
输出:
i
d
所以,它显然返回了字符。
现在我正在尝试另一个出现错误的程序:
type.cpp:8:46: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if(typeid((-1 + sqrt(1 - 8 * t)) / 2).name() == 'i')
程序是:
#include <math.h>
#include <iostream>
#include <typeinfo>
int main()
{
int t;
std::cin >> t;
if(typeid((-1 + sqrt(1 - 8 * t)) / 2).name() == 'i')
std::cout << "YES";
else
std::cout << "NO";
return 0;
}
为什么会出现这个错误?
【问题讨论】:
标签: c++