English 中文(简体)
const struct list::SingleList的前向声明,使用不完整类型list::SingleList是无效的(编译错误)
原标题:Forward declaration of const struct list::SingleList , Invalid use of incomplete type list::SingleList (Compilation errors)

单链表.h

#include "ListBase.h"
#include "DataNode.h"
#include "单一激光器。"

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

单一激光器。

#include "Iterator.h"
#include "单链表.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.

此外,只需使用一个类的前置声明就足够了,不需要同时使用两个类的前置声明。

我建议采用以下结构:

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: 单链表.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: 单链表.h

#include "SingleList.h"            // include full declarations of everything

// definitions

这样就没有文件互相包含,且所有类型在实现(.cpp)文件中都是完全知道的。

问题回答

问题是<代码>。 单一激光器:SingleListIterator 单一语文和册; 施工者需要了解<代码>head member of SingleList,因此需要全面申报这一类别。

你能:

  1. Move the constructor definition to a separate source file.
  2. 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.

此外,你们都包括了头文件并提供了前向声明。你只需要其中的一个(如果你只需要引用或指针到类型,并且不需要访问类型的成员变量或函数,请坚持使用前向声明)。

你正在正确的道路上一般解决这个问题。重要的部分是,如果Y.h也必须包括X.h,则X.h不包括Y.h。

你想将你的声明分离到头文件中,将你的定义分离到.cpp文件中。

页: 1

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

通常情况下,您也可以只使用前向声明就使用指针类型。

不要从 SingleList.h 中包含 SingleListIterator.h。在 SingleList.h 中的前向声明足够了。您不需要在 SingleList.h 中定义 SingleListIterator

(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.)





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签