【发布时间】:2018-03-30 17:48:16
【问题描述】:
我收到类似的错误
/hackerearth/CPP14_28/s_e3.cpp: In function ‘int main()’: /hackerearth/CPP14_28/s_e3.cpp:6:10: error: declaration of ‘auto x’ has no initializer auto x; ^
我的代码是,
#include <iostream>
using namespace std;
int main()
{
auto x;
cin >> x;
cout << x;
return 0;
}
我想要动态分配数据类型的类似功能
【问题讨论】:
-
你不能那样做。在 C++ 中,类型是在编译时确定的。
-
我希望在 c++14 preshing.com/20141202/cpp-has-become-more-pythonic 中是可能的。但我算没找到办法
-
你不能使用'auto x;'需要根据参考使用[en.cppreference.com/w/cpp/language/auto]
-
怎么办??
-
auto根据您初始化变量的方式确定变量的类型。没有初始化,没有类型。auto x = 0;,x 是int。auto x = 0.0;,x是double。auto x = Chainsaw(),x是Chainsaw。