錯誤 error C2143: syntax error : missing ',' before '<' 發生在 class AList : public List<Elem>,原因是 List 屬於 std 命名空間,未加 std:: 或 using namespace std;。
#include <list>
template <class Elem>
class AList : public List<Elem> { // 錯誤:List 未聲明
// ...
};
#include <list>
using namespace std;
template <class Elem>
class AList : public list<Elem> { ... };
#include <list>
template <class Elem>
class AList : public std::list<Elem> { ... };
listArrary / listArray / listArrary 混用,統一為 listArray。DefaultListSize 未定義,需定義常量或構造函數默認參數。rightLength() 調用但類中無此方法,應為 right()。.tpp 包含),否則鏈接錯誤。#include <list>
#include <iostream>
const int DEFAULT_SIZE = 100;
template <class Elem>
class AList : public std::list<Elem> {
private:
int maxSize, listSize, fence;
Elem* listArray;
public:
AList(int size = DEFAULT_SIZE) : maxSize(size), listSize(0), fence(0) {
listArray = new Elem[maxSize];
}
~AList() { delete[] listArray; }
// ... 其他方法實現直接寫在類內或頭文件下方
};
加上 std:: 即可解決編譯錯誤,並修正拼寫與邏輯問題使代碼可運行。