Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird Class Redefinition Error

Tags:

c++

templates

I'm having a weird error saying my class is getting redefined, stemming from the declaration of friend in my Node class. Here's my current code:

template <class T>
class Node {

private:

    T val;
    Node<T> * next;

public:

    friend class OList;

};

Any my other class:

template <class T>
class OList {  ------> Error here

private:

    Node<T> * head;
    int size;

public:

    OList();
    OList(const OList<T> &b);
    ~OList();

    clear();
    int size();
    T tGet(int input);
    int count(T input);
    insert (T input);
    remove (T input);
    uniquify(T input);
    Node<T> * returnHead();

};

// Constructs empty list
template <class T>
OList<T>::OList() { ---> Error here
    head = NULL;
    size = 0;
}
like image 943
user48944 Avatar asked Nov 12 '14 22:11

user48944


1 Answers

OList is not a class, it's a class template. You can either friend all specializations of the template:

template <typename> friend class OList;

or friend a specific specialization:

friend class OList<T>;

which will require OList to already be declared. Put a forward declaration before the definition of Node:

template <typename> class OList;
like image 109
Casey Avatar answered Nov 17 '22 01:11

Casey