Redirecting the declaration "const struct list :: SingleList", invalid use of the 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 the code Also, what can I do in such a case when there is a mutual relationship between the two header files ????? Thaaaaanks

+3
source share
4 answers

You use forward declarations, but you include files .hrecursively anyway . The point of the front declarations is that you do not need to include the headers of the forward declared class, thereby breaking the interdependence.

, .

:

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

, , , (.cpp).

+5

, SingleListIterator::SingleListIterator(const SingleList &) head SingleList, .

:

  • .
  • SingleList.h . SingleList.h , SingleListIterator.h.

, AND, . ( , ).

. , X.h Y.h, Y.h X.h.

+1

.cpp .

.cpp:

 SingleListIterator::SingleListIterator(const SingleList &list) : Iterator(list.head)
 {
 }

, .

0

Do not turn SingleListIterator.hon SingleList.h. A preliminary declaration is SingleList.henough for her . You do not need a definition SingleListIteratorin SingleList.h.

(I assume that you have some kind of “enable security” that you missed in the fragment.)
(I will let everyone else indicate everything that is bad in this fragment.)

0
source

Source: https://habr.com/ru/post/1734145/


All Articles