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

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

问题描述:

嗨我想解决这个错误,当我尝试将链接列表传递给另一个类时,我得到了。我已经看过其他问题,但他们似乎没有解决这个问题。我如何解决“[错误]使用不完整的类'SLLNode'”链接列表

我还需要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); 
//...... 

a ND“ListAsSLL.h”提前

+0

'SSLNode'被声明为嵌套类的' ListAsSLL“,但是在该范围之外声明前向。将它从ListAsSLL的作用域移出。 –

在你的类ListAsSLL,从外部类的点

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(); 
     ////////////// 

由于使用它,它只能访问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; 
+0

现在在我的其他类中,使用SLLNode的人说SLLNode没有指定类型 –

+0

@JaynillGopal这将解决您的问题,但允许喜欢列表的用户访问节点是有风险的。该解决方案打破封装并允许链表的用户修改列表的状态。例如,用户可能会恶意地或错误地使用'myNode.next = some_other_address;'并打破列表。 – user4581301

+0

@ user4581301 - 不,只有结构在公共部分。节点本身在受保护的部分 - 安全和声音:) –