创建具有通用值的列表时出现错误 "expression must have class type"
error "expression must have class type" when creating a list with generic values
我的customLinkedList哪里有问题???
我已经创建了 customLinkedList,它可以包含所有类型的值。
我不知道问题出在哪里...
在 main 的最后一行出现错误 "expression must have class type"。
有人可以帮助我吗?
#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");
}
我怀疑
List<string> l1();
应该是
List<std::string> l1;
string
可能在某处定义并因此导致错误。
我的customLinkedList哪里有问题??? 我已经创建了 customLinkedList,它可以包含所有类型的值。 我不知道问题出在哪里... 在 main 的最后一行出现错误 "expression must have class type"。 有人可以帮助我吗?
#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");
}
我怀疑
List<string> l1();
应该是
List<std::string> l1;
string
可能在某处定义并因此导致错误。