Forward declaration of 'const struct list::SingleList' , Invalid use of incomplete type 'list::SingleList' (Compilation errors)
SingleList.h
#include "ListBase.h"
#include "DataNode.h"
#include "SingleListIterator.h"
namespace list
{
开发者_如何学编程class SingleListIterator;
class SingleList : public ListBase
{
private:
DataNode *head;
DataNode *tail;
public:
SingleList();
SingleList(const SingleList &obj);
~SingleList();
void Flush(); //deletes all elements in the list
void PushInFront(const int data); // **
void Append(const int data); // **
void DeleteLast();
void DeleteFirst();
int Delete(const int& data); // ** remove the first occurrence of data and return 1 otherwise 0
const int& GetFirst() const; // **
int& GetFirst(); // **
const int& GetLast() const; // **
int& GetLast(); // **
void PrintList() const;
const int IsEmpty() const;
// SingleList<T> &operator=(const SingleList<T>& obj) (**)
// const int operator==(const SingleList<T> &obj) const (**)
// const int operator!=(const SingleList<T> &obj) const (**)
// SingleList<T>& operator+(const SingleList<T> &obj) (**) // concatenates two lists
// operator int() // returns list size (**)
friend class SingleListIterator; // ** ASK Changd it from Iterator
};
SingleListIterator.h
#include "Iterator.h"
#include "SingleList.h"
namespace list
{
class SingleList;
class SingleListIterator: public Iterator
{
public:
// error here --> Forward declaration of 'const struct list::SingleList'
SingleListIterator(const SingleList &list); // **
SingleListIterator(const SingleListIterator &obj); // **
virtual const int Current() const; // **
virtual void Succ();
virtual const int Terminate() const;
virtual void rewind();
// T &operator++(int) (**)
// SingleListIterator<T>& operator=(const SingleListIterator<T>&obj) (**)
};
// error here --> Invalid use of incomplete type 'list::SingleList'
SingleListIterator::SingleListIterator(const SingleList &list) : Iterator(list.head)
{
}
Errors indicated in code Also what can I do in a case like this where there is mutual coupling between two header files ????? Thaaaaanks
You use forward declarations, but you anyway include the .h
files recursively. The point of the forward declarations is that you don't need to include the headers of the
forward declared class, thereby breaking the mutual dependency.
Also it should be enough to use a forward declaration for one of the classes, not for both of them.
I would suggest the following structure:
SingleListIterator.h:
class SingleList; // forward declaration
class SingleListIterator {
// Declarations, only using pointers/references to SingleList.
// Definitions that need to know the structure of SingleList (like maybe
// a constructor implementation) need to be done in the .cpp file.
};
SingleList.h:
#include "SingleListIterator.h" // include full declaration
class SingleList {
// declarations
};
SingleListIterator.cpp:
#include "SingleListIterator.h"
#include "SingleList.h" // include full declaration of the type
// forward-declared in SingleListIterator.h
// method definitions,...
SingleList.h:
#include "SingleList.h" // include full declarations of everything
// definitions
This way there are no files that mutually include each other and all types are completely known in the implementation (.cpp) files.
The problem is that the SingleListIterator::SingleListIterator(const SingleList &)
constructor needs to know about the head
member of SingleList
, so it needs the full declaration of the class.
You can:
- Move the constructor definition to a separate source file.
- Just include SingleList.h instead of using a forward declaration. As long as SingleList.h is okay with a forward declaration, you don't also need to use one in SingleListIterator.h.
Also, you're both including the header files AND providing forward declarations. You only need one or the other (stick to a forward declaration if you only need references or pointers to the type and no access to the type's member variables or functions).
You're on the right track to solving this problem in general. The important part is that X.h doesn't include Y.h if Y.h also must include X.h.
You want to separate your declaration into header files and your definition into .cpp files.
Put this inside your .cpp:
SingleListIterator::SingleListIterator(const SingleList &list) : Iterator(list.head)
{
}
As a general rule, you can also always use a pointer type with just having the forward declaration.
Don't include SingleListIterator.h
from SingleList.h
. The forward declaration for it in SingleList.h
is sufficient. You don't need the definition of SingleListIterator
in SingleList.h
.
(I'm assuming you have some sort of "include guard" in place that you've omitted in the snippet.)
(I'll let everyone else point out all the other things that are poor about this snippet.)
精彩评论