c++ - 我如何解决 "[Error] invalid use of incomplete type ' 类 SLLNode'"链表

标签 c++

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

我还需要 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 中,从使用它的外部类的角度来看,它只能访问公共(public)成员。如您所见,您的 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;

关于c++ - 我如何解决 "[Error] invalid use of incomplete type ' 类 SLLNode'"链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40442498/

相关文章:

c++ - 处理长文件行的最佳多线程方案是什么?

C++ 模板实例化点困惑

c++ - LLVM 5.0 编译器是否等同于 GCC 编译器?

c++ - C++中如何判断传入缓冲区是否有效?

c++ - 为支持 OpenSSL 的 Windows Phone 8.1 编译 Qt 5.5 的问题

c++ - 我的辅音/元音替换器有什么错误?

c++ - 对于函数式转换或类型构造,预期为 '('

c++ - 等待所有线程完成一项工作然后再做另一项

c++ - 查找表示 GPS 路线的两条线之间的距离(MATLAB、Java、C++ 或 Python)

c++ - 理解(简单?)C++ 部分模板特化