繁体   English   中英

如何解决“[错误]无效使用不完整类型'类SLLNode'”链接列表

[英]How do I solve “[Error] invalid use of incomplete type 'class SLLNode'” Linked Lists

嗨,我正在努力解决这个错误,当我尝试将链接列表传递给我拥有的另一个类时,我得到了。 我已经查看了其他问题,但没有他们似乎解决了这个问题。

我还需要SLLNode保留在ListAsSLL中,因为它是其他类的父级

DynamicSizeMatrix应该具有ListAsSLL的聚合

我的DynamicSizeMatrix.cpp文件

#include "DynamicSizeMatrix.h"
#include<iostream>
#include<string>
#include<cassert>
#include<cstdlib>
#include"ListAsSLL.h"

using namespace std;

DynamicSizeMatrix::DynamicSizeMatrix(SLLNode* sll,int r, int *c)
{

    rws = r;
    col = new int [rws];
        for(int x=0; x < rws; x++) // sets the different row sizes
        {
            col[x] = c[x];
        }

    SLLNode *node = sll ;
//  node = sll.head;

    *info = new SLLNode*[rws];
    for (int x= 0; x< rws;x++)
    {
        info[x] = new SLLNode*[col[x]];

    }

    for(int x=0;x<rws;x++)
    {
        for(int y=0;y<col[x];y++)
        {
            info[x][y] = node;
            node = node->next; // My error happens here 

        }
    }


}

我的“DynamicSizeMatrix.h”

#include"ListAsSLL.h"   
#include "ListAsDLL.h"
#include"Matrix.h"
#include"Object.h"
class SLLNode;

class DynamicSizeMatrix : public Matrix
{

private:
    SLLNode*** info;
    //Object* colmns;
    int rws;
    int *col; //array of column sizes
    //  int size;

public:
    DynamicSizeMatrix()
    {
        info = 0;
        rws = 0;
        col = 0;
    }
    DynamicSizeMatrix(SLLNode* sll,int r, int *c);
//......

和“ListAsSLL.h”

class ListAsSLL : public List
{
    //friend class LLIterator;
    friend class DynamicSizeMatrix;
    protected:

        struct SLLNode
        {
                Object* info;
                SLLNode* next;
            //  SLLNode(Object *e, ListAsSLL::SLLNode* ptr = 0);
        };

        SLLNode* head;
        SLLNode* tail;
        SLLNode* cpos; //current postion;
        int count;

    public:

            ListAsSLL();

        ~ListAsSLL();
        //////////////

提前致谢

在您的类ListAsSLL中,从使用它的外部类的角度来看,它只能访问公共成员。 如您所见,您的SLLNode位于protected部分。

所以你真的有两个选择(好吧,实际上有很多选项),但是这里有两个选项将它封装在同一个文件中:

  1. 将SLLNode声明移动到public部分,然后使用它: ListAsSLL::SLLNode而不仅仅是SLLNode
  2. 只需将它移到类之外,使其可以访问包含“ListAsSLL.h”的所有人。

我更喜欢方法1,因为它使范围更小/更相关,但实际上没有多少......例如:

class ListAsSLL : public List
{   
    public:
        struct SLLNode
        {
                Object* info;
                SLLNode* next;
        };
        ListAsSLL();

    protected:
        SLLNode* head;
        SLLNode* tail;
        SLLNode* cpos; //current postion;
        int count;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM