bucket; de-6ren">
gpt4 book ai didi

c++ - 警告 "C++ requires a type specifier for all declaration"图

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:15 25 4
gpt4 key购买 nike

我在 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;
}

好像我在 main 里面做了同样的事情,它的工作完美

#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;
}

输出

1::=>3=>2=>1
2::
3::
Program ended with exit code: 0

这是我遗漏的东西吗?无法理解这种行为。任何建议、帮助或文档。我调查了类似的问题,但没有得到满意的答案

最佳答案

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

int i;
i = 100;

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

int i = 100;

或者在函数中设置值

int main() {
i = 100;
}

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

关于c++ - 警告 "C++ requires a type specifier for all declaration"图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45451081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com