Search code examples
c++classtemplatesincludeinclusion

Including a class in another class


So I'm trying to use the Node class that I just wrote in my LinkedList class but I'm getting the error that:

Symbol 'Node' could not be resolved

in the code below.

#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_

#include "Node.h"

template<class T>
class LinkedList {

    private:
        //Data Fields-----------------//
        Node<T> head;
        Node<T> tail;
};

#endif /* LINKEDLIST_H_ */

Node's declaration is below:

#ifndef NODE_H_
#define NODE_H_

template<class T>
class Node {

UPDATE:

So I still am having issues with my Node class being included in my LinkedList. But I discovered that by placing the two classes in one header file, I have no problems. So it must mean that the problem lies solely with the inclusion....which confuses me because that makes it seem like its some language based nuance that a beginner to C++ like me doesn't know about..


Solution

  • why do you have the semicolon after #include "Node.h" that's the problem.
    Edit: Things you can do to troubleshoot:

    1. Inline the class definition
      i.e. replace the # include statement with the actual definition of the class (for testing only)
    2. Check your header guards (the ones like # ifndef LINKEDLIST_H_
      try renaming them or removing them altogether (again for testing only)