MagicGAT
MagicGAT

Reputation: 135

How to template a data structure within a class all in-line (same .h file)

Ive been assigned to turn my linked list class into a template and I'm running into some confusion. I need to submit it as as a single .h file.

When I try to build i'm getting errors on every mention of LLnode, fwdPtr, and theData. That is every element of the structure so Ive done something very wrong there. Also, the struct definition itself is marked with a syntax error

template <class V>
class LL
{
    private:
        LLnode * header;
        struct <V> LLnode;
        {
            LLnode * fwdPtr; // has a pointer member
            V theData; // the data within the node
        };

    public:
        LL()
            {
                header = nullptr;
            }
        void push_front(string data)  
        {
            LLnode * new_node;
            new_node = new LLnode;                          
            new_node -> theData = data;                     

            if (header == nullptr)                      
            {                                               
                header = new_node;                    
                new_node -> fwdPtr = nullptr;
            }
            else                                            
            {
                LLnode * temp;                              
                temp = header;                          
                header = new_node;                      
                new_node -> fwdPtr = temp;                  
            }

            return;
        }
        .... more functions below ....

In the main(), upon which the function will be tested, a new linked list will be instantiated with <string> cast as the type. This is why I moved the struct LLnode inside the private member section of class LL. This is also why I am using V throughout the structure. Because that cast needs to reach down to the structure itself so when I dynamically allocate memory for nodes it will know to accept string data

I know I will need to change the function definitions to include V and use V throughout with some of the variables. But I don't understand where and why. Im confused on how template classes relate to pointer and programmer defined structures. I understand the simple examples of template classes/functions in my textbook but I am lost here.

Thanks in advance for any help!

EDIT: Here are the error messages im receiving (as requested)

../LL_template_class.h:23:3: error: unknown type name 'LLnode'
                LLnode * header;
                ^
../LL_template_class.h:24:3: error: declaration of anonymous struct must be a definition
                struct <V> LLnode;
                ^
../LL_template_class.h:24:3: warning: declaration does not declare anything [-Wmissing-declarations]
../LL_template_class.h:25:3: error: expected member name or ';' after declaration specifiers
                {
                ^
../LL_template_class.h:37:4: error: unknown type name 'LLnode'
                        LLnode * new_node;
                        ^
../LL_template_class.h:38:19: error: unknown type name 'LLnode'
                        new_node = new LLnode;
                                       ^
../LL_template_class.h:48:5: error: unknown type name 'LLnode'
                                LLnode * temp;

But like I said, I getting can not resolve errors on all mentions of my struc LLnode elements as well

Upvotes: 3

Views: 51

Answers (1)

OrdoFlammae
OrdoFlammae

Reputation: 731

Two problems:


First, you are ending your declaration of struct LLnode too early (you have an extra semicolon). You have

struct LLnode;
{
...
};

You should have

struct LLnode
{
...
};

Secondly, you have the declaration struct <V> LLnode, when you should have the declaration struct LLnode. Having the <V> there makes no syntactical sense.


Also, I'm not sure if this is necessary, but you may need to move your declaration of header below your declaration of LLnode, since header is defined as an LLnode.

Upvotes: 2

Related Questions