Question

Could anyone help me resolve the circular reference errors I'm getting here.

I've created my own deque class which is used by the breadthFirst method of FibTree.

Below are the highlights from the separate Header and CPP files. There are issues with invalid use of incomplete type and forward declaration error in FibTree files. I've marked these errors on the lines of code below.

deque.h

#ifndef DEQUE_H
#define DEQUE_H

#include "fibtree.h"

class dequeNode {
public:
    FibTree::Node* data;
};

class dequeList {
public:
    dequeNode* firstNode;
    dequeNode* lastNode;

    dequeList( void );

    void enque( FibTree::Node* );
    FibTree::Node* deque( void );
};

#endif

fibtree.h

#ifndef FIBTREE_H
#define FIBTREE_H

#include <iostream>

class dequeList; // ERROR: Forward declaration of 'struct dequeList' (2 ERRORS)

class FibTree {
public:
    class Node {
        ...
    };

    Node const* root; // 'root' pointer to constant Node

    FibTree (int);

    void breadthFirst(Node const* root);

};
#endif

fibtree.cpp

#include "fibtree.h"

void FibTree::breadthFirst(Node const* root) { // Breadth-first traversal
    dequeList* list = new dequeList(); //*** ERROR: Invalid use of incomplete type 'struct dequeList'
    list->enque(root);  //*** ERROR: Invalid use of incomplete type 'struct dequeList'
}

main.cpp

#include <iostream>

#include "deque.h"
#include "fibtree.h"

int main (int argc, const char* argv[]) {
    ...

I'd read on a similar post HERE, that the complete declaration should be included, in my case of dequeList, so I added #include "deque.h" above the forward declaration class deque.h", in fibtree.h; but this threw 16 compile errors to the deque.h class, such as these three errors: 'FibTree' has not been declared against FibTree::Node* data; inclass dequeNode {...`

Would anyone be able to highlight where I may be going wrong here?

Thanks, Alex

Was it helpful?

Solution

the file fibtree.h seems to have a missing #endif at the end , and add #include "deque.h" at the beginning of fibtree.cpp and in deque.h change void enque( FibTree::Node* ); to void enque( const FibTree::Node* )

OTHER TIPS

You must include deque.h in all source file where it's used. Otherwise you don't have the complete class definition.

The definition of the FibTree class does not depend on the dequeList class. There is no need to forward declare dequeList in fibtree.h.

Only the defintion of FibTree::breadthFirst depends on dequeList. When you implement this method, the FibTree class is already known and you only need to #include deque.h.

In fitree.cpp you are using your deque without including it's declaration. You should add #include "deque.h" in this file;

Also, fibtree.h should have a #endif in the end.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top