2013-02-15 102 views
0

我试图通过扩展抽象类linearList来定义具体类arrayList错误类型C2143 - 缺少','之前'<'

我的头用于定义的类如下: 类的ArrayList:公共linearList 我给出的误差在4号线:错误C2143:语法错误:‘之前<’失踪“”

当我通过扩展linearList定义时也会发生这种情况。

我确保使用名称空间标准并包含所有必需的文件。 linearList class没有错误,所以我认为这是arrayList的错误。

我也在第27行(代码中标记)出现错误“请参阅正在编译的类模板实例化”arrayList“的引用”。下面包括arrayListlinearList

的ArrayList

#include <iostream> 
using namespace std; 
template<class T> 
class arrayList : public linearList<T> 
{ 
public: 
    // constructor, copy constructor and destructor 
    arrayList(int initialCapacity = 10); 
    arrayList(const arrayList<T>&); 
    ~arrayList() {delete [] element;} 
    // ADT methods 
    bool empty() const {return listSize == 0;} 
    int size() const {return listSize;} 
    T& get(int theIndex) const; 
    int indexOf(const T& theElement) const; 
    void erase(int theIndex); 
    void insert(int theIndex, const T& theElement); 
    void output(ostream& out) const; 
    // additional method 
    int capacity() const {return arrayLength;} 
protected: 
    void checkIndex(int theIndex) const; 
    // throw illegalIndex if theIndex invalid 
    T* element;  // 1D array to hold list elements 
    int arrayLength;  // capacity of the 1D array 
    int listSize;   // number of elements in list 
}; //line 28 
template<class T> 
arrayList<T>::arrayList(int initialCapacity) 
{ 
    // Constructor. 
    if (initialCapacity < 1) 
    { 
     ostringstream s; 
     s << "Initial capacity = " << initialCapacity << " Must be > 0"; 
     throw illegalParameterValue(s.str()); 
    } 
    arrayLength = initialCapacity; 
    element = new T[arrayLength]; 
    listSize = 0; 
} 
template<class T> 
arrayList<T>::arrayList(const arrayList<T>& theList) 
{ 
    // Copy constructor. 
    arrayLength = theList.arrayLength; 
    listSize = theList.listSize; 
    element = new T[arrayLength]; 
    copy(theList.element, theList.element + listSize, element); 
} 
template<class T> 
void arrayList<T>::checkIndex(int theIndex) const 
{ 
    // Verify that theIndex is between 0 and 
    // listSize - 1. 
    if (theIndex < 0 || theIndex >= listSize) 
    { 
     ostringstream s; 
     s << "index = " << theIndex << " size = " 
      << listSize; 
     throw illegalIndex(s.str()); 
    } 
} 
template<class T> 
T& arrayList<T>::get(int theIndex) const 
{ 
    // Return element whose index is theIndex. 
    // Throw illegalIndex exception if no such 
    // element. 
    checkIndex(theIndex); 
    return element[theIndex]; 
} 
template<class T> 
int arrayList<T>::indexOf(const T& theElement)const 
{ 
    // Return index of first occurrence of theElement. 
     // search for theElement 
     int theIndex = (int) (find(element, element 
     + listSize, theElement) - element); 
    // check if theElement was found 
    if (theIndex == listSize) 
     return -1; // not found 
    else return theIndex; 
} 
template<class T> 
void arrayList<T>::erase(int theIndex) 
    {// Delete the element whose index is theIndex. 
    checkIndex(theIndex); 
    // valid index, shift elements with higher 
    // index 
    copy(element + theIndex + 1, element + 
    listSize,element + theIndex); 
    element[--listSize].~T(); // invoke destructor 
} 
template<class T> 
void arrayList<T>::insert(int theIndex, const T& theElement) 
{ 
    // Insert theElement. 
    if (theIndex < 0 || theIndex > listSize) 

    {// invalid index 
     // code to throw an exception comes here 
    } 
    // valid index, make sure we have space 
    if (listSize == arrayLength) 
    { 
     // no space, double capacity 
     changeLength1D(element, arrayLength, 
     2 * arrayLength); 
     arrayLength *= 2; 
    } 
    // shift elements right one position 
    copy_backward(element + theIndex, 
    element + listSize, 
    element + listSize + 1); 
    element[theIndex] = theElement; 
    listSize++; 
} 
template<class T> 
void arrayList<T>::output(ostream& out) const 
{ 
    // Put the list into the stream out. 
    copy(element, element + listSize, 
     ostream_iterator<T>(out, " ")); 
} 
template <class T> 
ostream& operator<<(ostream& out, const arrayList<T>& x) 
{x.output(out); return out;} 

LinearList

#include <ostream> 

using namespace std; 

template<class T> 
class linearList 
{ 
public: 
virtual ~linearList() {} 
virtual bool empty() const = 0; 
virtual int size() const = 0; 
virtual T& get(int theIndex) const = 0; 
virtual int indexOf(const T& theElement)const = 0; 
virtual void erase(int theIndex) = 0; 
virtual void insert(int theIndex,const T& theElement) = 0; 
virtual void output(ostream & out) const = 0; 
}; 
+3

你包括数组列表linearlist? – 2013-02-15 06:15:04

回答

2

洛瑞有正确的答案,但以防万一,目前还不清楚:从linearList

arrayList继承,所以这是不可能的定义arrayList无也知道linearList的定义。这就是编译器在第4行抱怨的原因;它还不知道linearListusing namespace std与此问题无关,顺便说一句,在头文件中有using语句是不好的做法。

假设你的“的ArrayList”代码示例是array_list.hlinear_list.h“LinearList”,你应该的array_list.h第几行改成这样:

#include <iostream> 
#include "linear_list.h" 

template<class T> 
class arrayList : public linearList<T> 
{ 
public: 
...