c++ - C2143 类型的错误 - 在 ',' 之前缺少 '<'

标签 c++ visual-studio

我试图通过扩展抽象类 linearList 来定义具体类 arrayList

我定义类的标题如下: 类 arrayList : public linearList 我在第 4 行收到错误:error C2143: syntax error : missing ',' before '<'

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

我确保使用 namespace std 并包含所有必要的文件。 linearList 类中没有错误,所以我认为这是 arrayList 中的错误。

我还在第 27 行(在代码中标记)收到错误“请参阅对正在编译的类模板实例化‘arrayList’的引用”。 arrayListlinearList 都包含在下面。

数组列表

#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;}

线性列表

#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;
};

最佳答案

lori 有正确答案,但以防万一不清楚:

arrayList 继承自 linearList,因此如果不知道 linearList 的定义,就不可能定义 arrayList。这就是编译器在第 4 行提示的原因;它还不知道 linearListusing namespace std 与这个问题无关,顺便说一下,在头文件中使用 using 语句是不好的做法。

假设您的“ArrayList”代码示例在array_list.h 中,“LinearList”在linear_list.h 中,您应该更改 的前几行array_list.h 对此:

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

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

关于c++ - C2143 类型的错误 - 在 ',' 之前缺少 '<',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14889243/

相关文章:

c++ - 快板 5 : trouble storing bitmaps in a std map

visual-studio - VS2017 RC - 尝试配置 IIS Express 时出现以下错误

c++ - 如何给现有项目添加CUDA 7.0加速?

c++ - 将我的 Visual Studio C++ 项目从 64 位系统移动到 32 位系统

c++ - 无法在 VS2015 中重定向项目

c++ - 如何确保客户端告诉服务器用户已断开连接,而不管用于关闭客户端的方法如何

c++ - CMake 和 Visual Studio 资源文件

c# - 如何从 C# 获取和发送 UTF-8 中的字符串到 C++ DLL

c++ - Qt:QSslCertificate::fromPath 找不到我的文件,但 QFileInfo 找到了

c++ - istringstream 对长数字进行四舍五入——我怎样才能防止这种情况发生?