1

In the function "Iteratoring List::begin()" { It has a problem " no matching constructor for initialization" for this Iteratoring(head). head is a node pointer and I built a constructor for it. I do not know what the problem is.

List.h

#include "Iteratoring.h"
struct Node {
    int data;       // value in the node
    Node *next;  //  the address of the next node

    /**************************************
            **      CONSTRUCTOR    **
    ***************************************/
    Node(int data) : data(data), next(0) {}
};
class List {
private:
    Node *head= nullptr;          // head node
    Node *tail;          // tail node
    Iteratoring begin();
public:
};

List.cpp

#include "List.h"

Iteratoring List::begin() {
    return Iteratoring(head);   //The error is here. no matching constructor for initialization
}

Iteratoring.h

#include "List.h"

class Iteratoring {
private:
    Node *current;
public:
    Iteratoring(){
        current= nullptr;
    };

    Iteratoring(Node *ptr){
        current=ptr;
    };

};
vahancho
  • 20,808
  • 3
  • 47
  • 55
Jonathan Sum
  • 48
  • 1
  • 6
  • It's the standard circular dependency problem: The file `List.h` includes `Iteratoring.h` which includes `List.h`. You solve this by *forward declarations* (my tip is to use forward declaration of `Node` in the `Iteratoring.h` file, and remove the inclusion of `List.h`). – Some programmer dude Apr 30 '19 at 11:43
  • Possible duplicate of [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – πάντα ῥεῖ Apr 30 '19 at 11:47

1 Answers1

0

This is a circular dependency issue. There's #include "List.h" in Iteratoring.h, and #include "Iteratoring.h" in List.h.

You should use forward declaration instead. e.g.

Iteratoring.h

class Node;
class Iteratoring {
private:
    Node *current;
public:
    Iteratoring(){
        current= nullptr;
    };

    Iteratoring(Node *ptr){
        current=ptr;
    };

};
songyuanyao
  • 169,198
  • 16
  • 310
  • 405