Search code examples
c++linked-listcustom-lists

error "expression must have class type" when creating a list with generic values


Where is the problem in my customLinkedList??? I have create that customLinkedList, that can contain all type of values. I don't know where is the problem... on the last line of the main appear the error "expression must have class type". Someone can help me?

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
/*
struct ha tutte le variabili pubbliche
*/

//CLASS NODE
template <class T>
class Node {
public:
    T data;
    Node<T>*next;
    Node();
    Node(T v);
    Node<T>(T v, Node *nextNode);
    T getValue();
};

template <class T>
Node<T>::Node() {
    data = null;
    next = nullptr;
}
template <class T>
Node<T>::Node(T v) {
    data = v;
    next = NULL;
}

template <class T>
Node<T>::Node(T v, Node *nextNode) {
    data = v;
    next = nextNode;
}
template <class T>
T Node<T>::getValue() {
    return data;
}

/*-------------------------------------------------------------------------------------------------*/

/*
class tutte le variabili private.
uso etichetta public: per avere variabili e funzioni pubbliche
*/

//CLASS LIST
template <class T>
class List {
    Node<T> *head;
public:
    List();
    ~List();
    void addFirst(T v);
    void deleteN(T v);
    Node<T> *find(T v);
};

template <class T>
List<T>::List() {
    head = NULL;
}

template <class T>
List<T>::~List() {
    Node<T> *tmp = head;
    while (tmp != NULL) {
        tmp = head->next;
        delete head;
        head = tmp;
    }
}

template <class T>
void List<T>::addFirst(T v) {
    Node<T> *n = new Node<T>();
    n->data = v;
    n->next = head;
    head = n;
}
template <class T>
Node<T>* List<T>::find(T v) {
    Node<T> * tmp = head;
    while (tmp->data != v && tmp != NULL) {
        tmp = tmp->next;
    }
    return tmp;
}

template <class T>
void List<T>::deleteN(T v) {
    Node<T> *iter = this->head;
    Node<T> *temp = iter;
    while (iter != NULL) {
        if (iter->data == data) {
            temp->next = iter->next;
            delete iter;
            break;
        }
        else {
            temp = iter;
            iter = iter->next;
        }
    }
    return;
}



int main() {
    Node<int> n1();
    Node<int> n2(5);
    Node<char> n3('z');
    char c = n3.getValue();
    printf("%c" , c);
 // with Node all work well...

    List<string> l1();
    l1.addFirst("Hello");
}

Solution

  • I suspect that

    List<string> l1();
    

    should be

    List<std::string> l1;
    

    string may be defined somewhere and hence cause the error.