自定义类型需要使用auto进行初始化的初始化程序吗?

我有这种类型:

  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

为什么会这样?是否因为它是指向Nodes的指针的向量

萨胡

使用auto nodeNode* node不使用auto Node* node

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

或者

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Kotlin:为什么我需要使用自定义getter初始化var?

重载>>运算符,并使用自定义类型的初始化列表初始化

使用@Namespace进行.matchedGeometryEffect的自定义初始化

使用自定义结构数据类型将数据传递到另一个视图控制器,需要初始化程序吗?

使用可选的初始化程序将自定义数据类型存储在@AppStorage中吗?

自定义初始化程序应使用哪种类型的参数?

从.sks文件使用自定义初始化程序进行子类化

需要帮助了解自定义uitableviewcell初始化

使用自定义初始化程序的Swift枚举会丢失rawValue初始化程序

自定义UIView是使用init(coder)初始化的,如何使用init(frame)对其进行初始化

在Go中初始化自定义int类型

开始,初始化自定义类型

最佳做法,如何初始化自定义类型?

自定义错误类型初始化器

Swift:自定义ViewController初始化程序

自定义绑定初始化

WorkManager-当我们同时使用默认初始化和自定义初始化时,是否应该删除默认初始化程序?

如何在keras自定义正则化程序中使用初始化的图层权重?

UILabel子类使用自定义颜色初始化

如何使用自定义属性初始化 SparkContext?

使用自定义ChangeNotifier初始化riverpod Provider

初始化前使用的自定义变量“层”

如何使用标准向量初始化自定义向量类型

使用Python进行Hive转换:无法初始化自定义脚本

如何使用进行错误检查的自定义设置器初始化属性?

致命错误:在自定义NavigationController中使用未实现的初始化程序

如何在UITableViewCell上使用自定义初始化程序?

如何在UICollectionViewCompositionalLayout中使用UICollectionViewController的自定义初始化程序?

无法使用自定义数据库初始化程序