0

I'm trying to build a template file for a class that is the node for a doubly linked list. Whenever I try to compile, I run into this error.

ISO C++ forbids declatation of "DNode" with no type

It seems to crop up in response to my functions that return DNode pointers. I've been working on this for days, and can't seem to make heads or tails of it.

#ifndef DNODE_H
#define DNODE_H
#include <cstdlib>
#include <string>
#include <iostream>
#include <iterator>

template <class T>
class DNode
{
    public:
        DNode(T StartingData = T(), DNode* PrevLink = NULL, DNode* NextLink = NULL)
            {Data = StartingData; previous = PrevLink; next = NextLink;}

        void setData(T item)
            {Data = item;}

        void setNext(DNode *l)
            {next = l;}
        void setPrevious(DNode *l)
            {previous = l;}

        *DNode getPrevious() {return previous;}
        *DNode getNext() {return next;}

        T getData()
            {return Data;}

    private:
        DNode *previous, *next;
        T Data;
};

#endif

1 Answers1

1

This is just a typo:

*DNode getPrevious() {return previous;}
*DNode getNext() {return next;}

should be:

DNode* getPrevious() {return previous;}
DNode* getNext() {return next;}

Other than that, code looks fine. You may want to work on your brace styling, some of your functions are hard to read, and your constructor would be better as a mem-initializer list. Also CapCase is typically only used for class names, for variables prefer camelCase (or snake_case, whichever). So I'd write your consturctor thusly:

DNode(T startingData = T(), DNode* prevLink = NULL, DNode* nextLink = NULL)
    : data(startingData)
    , previous(prevLink)
    , next(nextLink)
{ }
Barry
  • 286,269
  • 29
  • 621
  • 977
  • Wow, honestly a little embarrassed I spent that much time looking for something so small. Thanks for all the added advice too. Still in my first year of university, and I need to kill all those bad habits – Connor Abla Mar 21 '16 at 02:40