samedi 23 mai 2015

how to remove error : X is not a class template

I'm no expert in using templates but I'm not sure why I'm getting error: 'SLinked_List' is not a class template: friend class SLinked_List<T>; in the definition of class SNode. What is wrong with this piece of code?

Thank you, Pranav

#include <iostream>
#include <string>

template <typename T>
class SNode{
    friend class SLinked_List<T>;
private:
    T data;
    SNode<T>* next;
};

template <typename T>
class SLinked_List{

private:
    SNode<T>* head;

public:
    SLinked_List(){
        head = nullptr;
    }

    bool empty() const { return head == nullptr; }

    void insert_first (const T&);

};

template <typename T>
void SLinked_List<T> :: insert_first (const T& t){
    SNode<T>* node = new SNode<T>;
    node->data = t;
    node->next = head;
    head = node;
}

int main(){

    SLinked_List<std::string> ls;

    ls.insert_first("Hello");

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire