Search code examples
c++templatesmost-vexing-parse

Issue trying to use a template object inside a template class


Consider the following class definitions ...

Node

    template <class T> class Node {
        private :
            T* data;
            Node<T>* next;
        public :
            Node(T* data);
            void setData(T* data);
            T* getData();
            void setNext(Node<T>* next);
            Node<T>* getNext();
    };

Linked List

    template <class T> class LinkedList {
        private :
            Node<T>* start;
        public :
            LinkedList();
            void add(Node<T>* node);
            bool isEmpty();
    };

Main

    #include "Foo.h"
    int main() {
        Foo foo();
        Node<Foo> node(&foo);
        LinkedList<Foo> linkedList();
        linkedList.add(&node);
        return 0;
    }

When it is compiled it throws the following error...

    Request for member 'add' in 'linkedList', which is of non-class type 'LinkedList<Foo>()'

I am quite inexperienced making use of templates, so any help would be greatly appreciated.


Solution

  • Foo foo(); and LinkedList<Foo> linkedList(); are not variables but are function prototypes.

    Please use Foo foo; and LinkedList<Foo> linkedList; instead.

    You can also use Foo foo = Foo(); and LinkedList<Foo> linkedList = LinkedList<Foo>(); to make it clear that the constructors are called.