Jaynill Gopal Jaynill Gopal - 1 month ago 6
C++ Question

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

Hi I'm trying so solve this error I am getting when i try to pass a Linked list to another class that i have. I have looked through other questions but non of them seem to fix this problem.

I also need SLLNode to stay in ListAsSLL because it is a parent to other classes

DynamicSizeMatrix is supposed to have an aggregation to ListAsSLL

My DynamicSizeMatrix.cpp file

#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

}
}


}


my "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);
//......


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


Thanks in advance

Answer

In your class ListAsSLL, from the point of view of an external class using it, it only has access to the public members. As you can see your SLLNode is in the protected section.

So you have two options really (well, actually there are quite a few), but here are two that keep it encapsulated in the same file as least:

  1. Move SLLNode declaration into a public section and then use it like: ListAsSLL::SLLNode instead of just SLLNode.
  2. Just move it outside of the class to make it accesable to all who include "ListAsSLL.h".

I prefer method 1 since it keeps the scope smaller/more relevant, but there is not much in it really.... eg:

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

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