gpt4 book ai didi

c++ - 自定义类型需要一个初始化程序来声明自动?

转载 作者:行者123 更新时间:2023-11-28 02:02:53 25 4
gpt4 key购买 nike

我有这种类型:

  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 的指针 vector 吗?秒?

最佳答案

使用 auto nodeNode* node,而不是 auto Node* node

for (Node* node : children) {
node->decrementDependency();
}

for (auto node : children) {
node->decrementDependency();
}

关于c++ - 自定义类型需要一个初始化程序来声明自动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38670471/

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