Jon Claus
Jon Claus

Reputation: 2932

Error of type C2143 - Missing ',' before '<'

I am attempting to define the concrete class arrayList by extending the abstract class linearList.

My header for defining the class is as follows: class arrayList : public linearList I am given the error at line 4: error C2143: syntax error : missing ',' before '<'

This also happens when I define chain by extending linearList.

I made sure to use namespace std and include all necessary files. There are no errors in the linearList class so I think it's an error in arrayList.

I am also getting the error "see reference to class template instantiation 'arrayList' being compiled" at line 27 (marked in code). Both arrayList and linearList are included below.

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

Upvotes: 0

Views: 707

Answers (1)

congusbongus
congusbongus

Reputation: 14687

lori has the right answer, but just in case it's not clear:

arrayList inherits from linearList, so it's impossible to define arrayList without also knowing the definition of linearList. This is why the compiler is complaining at line 4; it doesn't yet know about linearList. using namespace std is irrelevant to this problem, and by the way it's bad practice to have using statements in header files.

Assuming that your "ArrayList" code sample is in array_list.h and "LinearList" in linear_list.h, you should change the first few lines of array_list.h to this:

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

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

Upvotes: 2

Related Questions