我采用的Linux系统gcc编译器,编译程序的时候出现"error: expected template-name before '<' token",自己搞不懂是哪里出了问题,非常感谢。代码粘贴的有点多,望见谅。
相关代码
// 文件test.c++
#include <iostream>
#include "List/LList.h"
using namespace std;
#include <stdlib.h>
int
main ( int argc, char *argv[] ) {
LList<int> list;
for (int i = 1; i <= 10; i ++)
{
list.append(i);
}
cout << "size of first: " << alist.length() << endl;
return EXIT_SUCCESS;
}
// 文件LList.h
#include<assert.h>
#include<iostream>
#define defaultSize 100
using namespace std;
// Singly linked list node
template <typename E>
class Link {
public:
E element;
Link *next;
Link(const E& elemval, Link *nextval = NULL) {
element = elemval;
next = nextval;
}
Link(Link* nextval = NULL) {
next = nextval;
}
};
// Linked List implementation
template <typename E>
class LList: public List<E> {
private:
Link<E>* head;
Link<E>* tail;
Link<E>* curr;
int cnt;
void init() {
curr = tail = head = new Link<E>;
cnt = 0;
}
void removeall() {
while(head != NULL) {
curr = head;
head = head->next;
delete curr;
}
}
public:
// Constructor
LList(int size = defaultSize) { init(); }
// Destructor
~LList() { removeall(); }
// Print list contents
void print() const;
// Clear list;
void clear() { removeall();init(); }
// Insert "it" at current position
void insert(const E& it) {
curr->next = new Link<E>(it, curr->next);
if(tail == curr) tail = curr->next;
cnt ++;
}
// Append "it" to list
void append(const E& it) {
tail = tail->next = new Link<E>(it, NULL);
cnt ++;
}
// Remove and return current element
E remove() {
Assert(curr->next != NULL && "No element");
// Remember value
E it = curr->next->element;
// Remember link node
Link<E> *ltemp = curr->next;
// Reset tail
if(tail == curr->next) tail = curr;
// Remove from list
curr->next = curr->next->next;
// Reclaim space
delete ltemp;
cnt --;
return it;
}
// Put curr at start of list
void moveToStart() {
curr = head;
}
// Put curr at end of list
void moveToEnd() {
curr = tail;
}
// Put curr one step at left, no change if already at front
void prev() {
if(curr == head) return;
Link<E>* temp = head;
while(temp->next != curr) temp = temp->next;
curr = temp;
}
// Move curr one step at right, nochange if already at last
void next() {
if(curr != tail) curr = curr->next;
}
// Return length
int length() const { return cnt; }
// Return position of current element
int currPos() const {
Link<E>* temp = head;
int i;
for(i = 0; curr != temp; i ++) {
temp = temp->next;
}
return i;
}
// Move current to "pos"
void moveToPos(int pos) {
Assert((pos >= 0) && (pos <= cnt) && "Position out of range");
curr = head;
for(int i = 0; i < pos; i ++) curr = curr->next;
}
// Return current value
const E& getValue() const {
Assert(curr->next != NULL && "No value");
return curr->next->element;
}
};
报错信息
// Linked List implementation
template <typename E>
class LList: public List<E> {
代码这里编译出现错误:error: expected template-name before '<' token
相关截图
自己已经百度相关template相关解答。
class LList: public List {
这里的List没有定义吧