警告"C++需要所有声明的类型说明符"映射

Har*_*ngh 1 c++ dictionary stl deque

我在xcode中运行此代码.为什么我的编译器继续抱怨地图分配

#include <iostream>
#include <map>
#include <deque>
using namespace std;

map<int,deque<int>> bucket;
deque<int> A{3,2,1};
deque<int> B;
deque<int> C;


bucket[1] = A;//Warning "C++ requires a type specifier for all declaration
bucket[2] = B;//Warning "C++ requires a type specifier for all declaration
bucket[3] = C;//Warning "C++ requires a type specifier for all declaration

int main() {

    for (auto it:bucket)
    {
        cout << it.first << "::";
        for (auto di = it.second.begin(); di != it.second.end(); di++)
        {
            cout << "=>" << *di;
        }
        cout << endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

好像我在主内部做同样的事情,它的作品完美无缺

#include <iostream>
#include <map>
#include <deque>
using namespace std;

map<int,deque<int>> bucket;
deque<int> A{3,2,1};
deque<int> B;
deque<int> C;  

int main() {

    bucket[1] = A;
    bucket[2] = B;
    bucket[3] = C;

    for (auto it:bucket)
    {
        cout << it.first << "::";
        for (auto di = it.second.begin(); di != it.second.end(); di++)
        {
            cout << "=>" << *di;
        }
        cout << endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

产量

1::=>3=>2=>1
2::
3::
Program ended with exit code: 0
Run Code Online (Sandbox Code Playgroud)

这是我失踪的东西.无法理解这种行为.任何建议,帮助或文档.我看了类似的问题,但没有得到满意的答案

Dak*_*ksh 5

你不能在全球范围内做这样的事情

int i;
i = 100;
Run Code Online (Sandbox Code Playgroud)

因为在C++ 11中,您可以在声明时初始化值,如下所示

int i = 100;
Run Code Online (Sandbox Code Playgroud)

或者在函数内设置值

int main() {
 i = 100;
}
Run Code Online (Sandbox Code Playgroud)

STL初始化也是如此,这就是您遇到问题的原因