vendredi 29 juillet 2016

custom type requires an initializer to declare with auto?

I have this type:

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

and I am trying to iterate through a vector<Node*> in a range-based loop, like so:

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

However, the compiler gives this error, error: declaration of variable 'Node' with type 'auto' requires an initializer.

Why does this happen? Is it because it's a vector of pointers to Nodes?

Aucun commentaire:

Enregistrer un commentaire