Xcode中的显式数字类型 这里是一个C++代码,我在XCODE中编写,编译时显示一个错误消息 #include <complex> using namespace std; int main(int argc, const char * argv[]) { complex<float> a(1,1); cout<<1.0+a<<endl; }

Xcode中的显式数字类型 这里是一个C++代码,我在XCODE中编写,编译时显示一个错误消息 #include <complex> using namespace std; int main(int argc, const char * argv[]) { complex<float> a(1,1); cout<<1.0+a<<endl; },c++,C++,也就是说,编译器将1.0作为双精度类型。但我想要的是一个浮点数,我能做什么 谢谢。默认情况下,浮点文字值(如1.0和200.0)为双精度类型。您可以通过在代码中的文本值后面附加一个f来强制它们键入float,如下所示 Invalid operands to binary operation('double' and 'complex<float>') 用于谷歌搜索的标准术语是文字,而不是明确的。 std::cout << 1.0f + a << std::e

也就是说,编译器将1.0作为双精度类型。但我想要的是一个浮点数,我能做什么


谢谢。

默认情况下,浮点文字值(如1.0和200.0)为双精度类型。您可以通过在代码中的文本值后面附加一个f来强制它们键入float,如下所示

Invalid operands to binary operation('double' and 'complex<float>')

用于谷歌搜索的标准术语是文字,而不是明确的。
std::cout << 1.0f + a << std::endl;
               _^_