0
我有这样的类型:自定义类型需要初始化程序来声明与自动?
9 class Node {
10 string name;
11 int dependencies;
12 vector<Node*> children;
13 unordered_map<string, Node*> map;
14
15
16 public:
17 Node(string name) : name(name) {}
18 void decrementDependency() { dependencies--;}
19 void incrementDependency() { dependencies++;}
20 string getName() { return name; }
21 vector<Node*> getChildren() { return children; }
22 int getNumDependencies() { return dependencies; }
23
24 void addDependent(Node* node) {
25 map[node->getName()] = node;
26 children.push_back(node);
27 node->incrementDependency();
28 }
29 };
,我试图通过vector<Node*>
在基于范围的循环迭代,像这样:
for (auto Node* node : children) {
node->decrementDependency();
}
然而,编译器为这个错误,error: declaration of variable 'Node' with type 'auto' requires an initializer
。
为什么会发生这种情况?是因为它是一个指向Node
的向量吗?
得到它 - 感谢! –
@LeibnizMan,不客气。快乐编程! –
为了更好地表明,“children”是指针的容器,我会建议使用'auto * node' – Zereges