title: cs106b library vector.cpp error C2143: syntax error : missing ';' before '<'
body: <p>relating to <code>Vector&lt;ElemType&gt;::Vector(int capacity)</code></p>
  
  <ul>
  <li>C2143: syntax error : missing ';' before '&lt;'</li>
  <li>C4430: missing type specifier - int assumed. Note: C++ does not support default-int</li>
  <li>C2988: unrecognizable template declaration/definition </li>
  <li>C2059: syntax error : '&lt;'</li>
  </ul>
  
  <p>relating to <code>Vector&lt;ElemType&gt;::~Vector()</code></p>
  
  <ul>
  <li>C2588: '::~Vector' : illegal global destructor</li>
  </ul>
  
  
  
  <pre><code>/*
   * File: private/vector.cpp
   * Last modified on Fri Jun  5 16:19:42 2009 by eroberts
   * -----------------------------------------------------
   * This file contains the implementation of the vector.h interface.
   * Because of the way C++ compiles templates, this code must be
   * available to the compiler when it reads the header file.
   */
  
  #ifdef _vector_h
  
  #include "stdafx.h"
  
  /*
   * Vector class implementation
   * ---------------------------
   * The Vector is internally managed as a dynamic array of elements.
   * It tracks capacity (numAllocated) separately from size (numUsed).
   * All access is bounds-checked for safety.
   */
  
  template &lt;typename ElemType&gt;
  Vector&lt;ElemType&gt;::Vector(int capacity) {
      elements = new ElemType[capacity];
      numAllocated = capacity;
      numUsed = 0;
      timestamp = 0L;
  }
  
  template &lt;typename ElemType&gt;
  Vector&lt;ElemType&gt;::~Vector() {
      if (elements != NULL) delete[] elements;
  }
  
  template &lt;typename ElemType&gt;
  inline int Vector&lt;ElemType&gt;::size() {
      return numUsed;
  }
  
  template &lt;typename ElemType&gt;
  bool Vector&lt;ElemType&gt;::isEmpty() {
      return size() == 0;
  }
  
  template &lt;typename ElemType&gt;
  ElemType Vector&lt;ElemType&gt;::getAt(int index) {
      checkRange(index, "getAt");
      return elements[index];
  }
  
  template &lt;typename ElemType&gt;
  void Vector&lt;ElemType&gt;::setAt(int index, ElemType elem) {
      checkRange(index, "setAt");
      elements[index] = elem;
  }
  
  /* Private method: checkRange
   * --------------------------
   * Verifies that index is in range for this vector, if not, raises an
   * error.  The verb string is used in the error message to describe the
   * operation that caused the range error, .e.g "setAt" or "removeAt".
   */
  
  template &lt;typename ElemType&gt;
  inline void Vector&lt;ElemType&gt;::checkRange(int index, const char *verb) {
      if (index &lt; 0 || index &gt;= size()) {
          Error("Attempt to " + string(verb) + " index "
                + IntegerToString(index) + " in a vector of size "
                + IntegerToString(size()) + ".");
      }
  }
  
  template &lt;typename ElemType&gt;
  inline ElemType &amp;Vector&lt;ElemType&gt;::operator[](int index) {
      checkRange(index, "access");
      return elements[index];
  }
  
  template &lt;typename ElemType&gt;
  void Vector&lt;ElemType&gt;::add(ElemType elem) {
      insertAt(numUsed, elem);
  }
  
  template &lt;typename ElemType&gt;
  void Vector&lt;ElemType&gt;::insertAt(int index, ElemType elem) {
      if (numAllocated == numUsed) enlargeCapacity();
      if (index != numUsed) checkRange(index, "insertAt");
      for (int i = numUsed; i &gt; index; i--) {
          elements[i] = elements[i-1];
      }
      elements[index] = elem;
      numUsed++;
      timestamp++;
  }
  
  template &lt;typename ElemType&gt;
  void Vector&lt;ElemType&gt;::removeAt(int index) {
      checkRange(index, "removeAt");
      for (int i = index; i &lt; numUsed-1; i++) {
          elements[i] = elements[i+1];
      }
      numUsed--;
      timestamp++;
  }
  
  template &lt;typename ElemType&gt;
  void Vector&lt;ElemType&gt;::clear() {
      delete[] elements;
      elements = NULL;
      numUsed = numAllocated = 0;
      timestamp++;
  }
  
  template &lt;typename ElemType&gt;
  const Vector&lt;ElemType&gt; &amp;Vector&lt;ElemType&gt;::operator=(const Vector &amp; rhs) {
      if (this != &amp;rhs) {
          clear();
          copyInternalData(rhs);
          timestamp = 0L;
      }
      return *this;
  }
  
  template &lt;typename ElemType&gt;
  Vector&lt;ElemType&gt;::Vector(const Vector &amp; rhs) {
      copyInternalData(rhs);
      timestamp = 0L;
  }
  
  template &lt;typename ElemType&gt;
  void Vector&lt;ElemType&gt;::mapAll(void (*fn)(ElemType)) {
      long t0 = timestamp;
      for (int i = 0; i &lt; numUsed; i++) {
          if (timestamp != t0) {
              Error("Vector structure has been modified");
          }
          fn(elements[i]);
      }
  }
  
  template &lt;typename ElemType&gt;
  template &lt;typename ClientDataType&gt;
  void Vector&lt;ElemType&gt;::mapAll(void (*fn)(ElemType, ClientDataType&amp;),
                                ClientDataType &amp; data) {
      long t0 = timestamp;
      for (int i = 0; i &lt; numUsed; i++) {
          if (timestamp != t0) {
              Error("Vector structure has been modified");
          }
          fn(elements[i], data);
      }
  }
  
  /*
   * Vector::Iterator class implementation
   * ----------------------------------
   * The Iterator for Vector maintains a pointer to the original Vector and
   * an index into that vector that identifies the next element to return.
   */
  
  template &lt;typename ElemType&gt;
  Vector&lt;ElemType&gt;::Iterator::Iterator() {
      vp = NULL;
  }
  
  template &lt;typename ElemType&gt;
  typename Vector&lt;ElemType&gt;::Iterator Vector&lt;ElemType&gt;::iterator() {
      return Iterator(this);
  }
  
  template &lt;typename ElemType&gt;
  Vector&lt;ElemType&gt;::Iterator::Iterator(Vector *vecRef) {
      vp = vecRef;
      curIndex = 0;
      timestamp = vp-&gt;timestamp;
  }
  
  template &lt;typename ElemType&gt;
  bool Vector&lt;ElemType&gt;::Iterator::hasNext() {
      if (vp == NULL) Error("hasNext called on uninitialized iterator");
      if (timestamp != vp-&gt;timestamp) {
          Error("Vector structure has been modified");
      }
      return curIndex &lt; vp-&gt;size();
  }
  
  template &lt;typename ElemType&gt;
  ElemType Vector&lt;ElemType&gt;::Iterator::next() {
      if (vp == NULL) Error("next called on uninitialized iterator");
      if (!hasNext()) {
          Error("Attempt to get next from iterator"
                " where hasNext() is false");
      }
      return (*vp)[curIndex++];
  }
  
  template &lt;typename ElemType&gt;
  ElemType Vector&lt;ElemType&gt;::foreachHook(FE_State &amp; fe) {
      if (fe.state == 0) fe.iter = new Iterator(this);
      if (((Iterator *) fe.iter)-&gt;hasNext()) {
          fe.state = 1;
          return ((Iterator *) fe.iter)-&gt;next();
      } else {
          fe.state = 2;
          return ElemType();
      }
  }
  
  /* Private method: enlargeCapacity
   * -------------------------------
   * Doubles the current capacity of the vector's internal storage,
   * copying all existing values.
   */
  
  template &lt;typename ElemType&gt;
  void Vector&lt;ElemType&gt;::enlargeCapacity() {
      numAllocated = (numAllocated == 0 ? 10 : numAllocated*2);
      ElemType *newArray = new ElemType[numAllocated];
      for (int i = 0; i &lt; numUsed; i++) {
          newArray[i] = elements[i];
      }
      delete[] elements;
      elements = newArray;
  }
  
  /* Private method: copyInternalData
   * --------------------------------
   * Common code factored out of the copy constructor and operator= to
   * copy the contents from the other vector.
   */
  
  template &lt;typename ElemType&gt;
  void Vector&lt;ElemType&gt;::copyInternalData(const Vector &amp; other) {
      elements = new ElemType[other.numUsed];
      for (int i = 0; i &lt; other.numUsed; i++) {
          elements[i] = other.elements[i];
      }
      numUsed = other.numUsed;
      numAllocated = other.numUsed;
  }
  
  #endif
  </code></pre>
  
  <p>example of vector class defind</p>
  
  <pre><code>/*
   * File: set.h
   * Last modified on Thu Jun 11 09:17:43 2009 by eroberts
   *      modified on Tue Jan  2 14:34:06 2007 by zelenski
   * -----------------------------------------------------
   * This interface file contains the Set class template, a
   * collection for efficiently storing a set of distinct elements.
   */
  
  #ifndef _set_h
  #define _set_h
  
  #include "cmpfn.h"
  #include "bst.h"
  #include "vector.h"
  #include "foreach.h"
  
  /*
   * Class: Set
   * ----------
   * This interface defines a class template that stores a collection of
   * distinct elements, using a sorted relation on the elements to
   * provide efficient managaement of the collection.
   * For maximum generality, the Set is supplied as a class template.
   * The element type is determined by the client. The client configures
   * the set to hold values of a specific type, e.g. Set&lt;int&gt; or
   * Set&lt;studentT&gt;. The one requirement on the element type is that the
   * client must supply a comparison function that compares two elements
   * (or be willing to use the default comparison function that uses
   * the built-on operators  &lt; and ==).
   */
  
  template &lt;typename ElemType&gt;
  class Set {
  
  public:
  
  /* Forward references */
      class Iterator;
  
  /*
   * Constructor: Set
   * Usage: Set&lt;int&gt; set;
   *        Set&lt;student&gt; students(CompareStudentsById);
   *        Set&lt;string&gt; *sp = new Set&lt;string&gt;;
   * -----------------------------------------
   * The constructor initializes an empty set. The optional
   * argument is a function pointer that is applied to
   * two elements to determine their relative ordering. The
   * comparison function should return 0 if the two elements
   * are equal, a negative result if first is "less than" second,
   * and a positive resut if first is "greater than" second. If
   * no argument is supplied, the OperatorCmp template is used as
   * a default, which applies the bulit-in &lt; and == to the
   * elements to determine ordering.
   */
      Set(int (*cmpFn)(ElemType, ElemType) = OperatorCmp);
  
  /*
   * Destructor: ~Set
   * Usage: delete sp;
   * -----------------
   * The destructor deallocates  storage associated with set.
   */
      ~Set();
  
  /*
   * Method: size
   * Usage: count = set.size();
   * --------------------------
   * This method returns the number of elements in this set.
   */
      int size();
  
  /*
   * Method: isEmpty
   * Usage: if (set.isEmpty())...
   * ----------------------------
   * This method returns true if this set contains no
   * elements, false otherwise.
   */
      bool isEmpty();
  
  /*
   * Method: add
   * Usage: set.add(value);
   * ----------------------
   * This method adds an element to this set. If the
   * value was already contained in the set, the existing entry is
   * overwritten by the new copy, and the set's size is unchanged.
   * Otherwise, the value is added and set's size increases by one.
   */
      void add(ElemType elem);
  
  /*
   * Method: remove
   * Usage: set.remove(value);
   * -----------------------
   * This method removes an element from this set. If the
   * element was not contained in the set, the set is unchanged.
   * Otherwise, the element is removed and the set's size decreases
   * by one.
   */
      void remove(ElemType elem);
  
  /*
   * Method: contains
   * Usage: if (set.contains(value))...
   * -----------------------------------
   * Returns true if the element in this set, false otherwise.
   */
      bool contains(ElemType elem);
  
  /*
   * Method: find
   * Usage: eptr = set.find(elem);
   * -----------------------------
   * If the element is contained in this set, returns a pointer
   * to that elem.  The pointer allows you to update that element
   * in place. If element is not contained in this set, NULL is
   * returned.
   */
      ElemType *find(ElemType elem);
  
  /*
   * Method: equals
   * Usage: if (set.equals(set2)) . . .
   * -----------------------------------
   * This predicate function implements the equality relation
   * on sets.  It returns true if this set and set2 contain
   * exactly the same elements, false otherwise.
   */
      bool equals(Set &amp; otherSet);
  
  /*
   * Method: isSubsetOf
   * Usage: if (set.isSubsetOf(set2)) . . .
   * --------------------------------------
   * This predicate function implements the subset relation
   * on sets.  It returns true if all of the elements in this
   * set are contained in set2.  The set2 does not have to
   * be a proper subset (that is, it may be equals).
   */
      bool isSubsetOf(Set &amp; otherSet);
  
  /*
   * Methods: unionWith, intersectWith, subtract
   * Usage: set.unionWith(set2);
   *        set.intersectWith(set2);
   *        set.subtract(set2);
   * -------------------------------
   * These fmember unctions modify the receiver set as follows:
   *
   * set.unionWith(set2);      Adds all elements from set2 to this set.
   * set.intersectWith(set2);  Removes any element not in set2 from this set.
   * set.subtract(set2);       Removes all element in set2 from this set.
   */
      void unionWith(Set &amp; otherSet);
      void intersectWith(Set &amp; otherSet);
      void subtract(Set &amp; otherSet);
  
  /*
   * Method: clear
   * Usage: set.clear();
   * -------------------
   * This method removes all elements from this set. The
   * set is made empty and will have size() = 0 after being cleared.
   */
      void clear();
  
  /*
   * SPECIAL NOTE: mapping/iteration support
   * ---------------------------------------
   * The set supports both a mapping operation and an iterator which
   * allow the client access to all elements one by one.  In general,
   * these  are intended for _viewing_ elements and can behave
   * unpredictably if you attempt to modify the set's contents during
   * mapping/iteration.
   */
  
  /*
   * Method: mapAll
   * Usage: set.mapAll(Print);
   * -------------------------
   * This method iterates through this set's contents
   * and calls the function fn once for each element.
   */
      void mapAll(void (*fn)(ElemType elem));
  
  /*
   * Method: mapAll
   * Usage: set.mapAll(PrintToFile, outputStream);
   * --------------------------------------------
   * This method iterates through this set's contents
   * and calls the function fn once for each element, passing
   * the element and the client's data. That data can be of whatever
   * type is needed for the client's callback.
   */
      template &lt;typename ClientDataType&gt;
      void mapAll(void (*fn)(ElemType elem, ClientDataType &amp; data),
                  ClientDataType &amp; data);
  
  /*
   * Method: iterator
   * Usage: iter = set.iterator();
   * -----------------------------
   * This method creates an iterator that allows the client to
   * iterate through the elements in this set.  The elements are
   * returned in the order determined by the comparison function.
   *
   * The idiomatic code for accessing elements using an iterator is
   * to create the iterator from the collection and then enter a loop
   * that calls next() while hasNext() is true, like this:
   *
   *     Set&lt;int&gt;::Iterator iter = set.iterator();
   *     while (iter.hasNext()) {
   *         int value = iter.next();
   *         . . .
   *     }
   *
   * This pattern can be abbreviated to the following more readable form:
   *
   *     foreach (int value in set) {
   *         . . .
   *     }
   *
   * To avoid exposing the details of the class, the definition of the
   * Iterator class itself appears in the private/set.h file.
   */
      Iterator iterator();
  
  private:
  
  #include "private/set.h"
  
  };
  
  #include "private/set.cpp"
  
  #endif
  </code></pre>
  
  <p>This <a href="http://www.stanford.edu/class/cs106x/documentation/vector_8h.html" rel="nofollow">class Vector</a> is a standard component of a library, so it should compile - intuition is that either a header is missing or the compiler settings are wrong. I obtained this copy from the <a href="http://sourceforge.net/projects/progabstrlib/files/" rel="nofollow">sourceforge CS106B library</a></p>
  
  <p>Also, here are the vector.h public and private files</p>
  
  <pre><code>/*
   * File: private/vector.h
   * Last modified on Fri Jun  5 15:39:26 2009 by eroberts
   * -----------------------------------------------------
   * This file contains the private section of the vector.h interface.
   * This portion of the class definition is taken out of the vector.h
   * header so that the client need not have to see all of these
   * details.
   */
  
  public:
  
  /*
   * Class: Vector&lt;ElemType&gt;::Iterator
   * ---------------------------------
   * This interface defines a nested class within the Vector template that
   * provides iterator access to the Vector contents.
   */
      class Iterator : public FE_Iterator {
      public:
          Iterator();
          bool hasNext();
          ElemType next();
  
      private:
          Iterator(Vector *vecRef);
          Vector *vp;
          int curIndex;
          long timestamp;
          friend class Vector;
      };
      friend class Iterator;
      ElemType foreachHook(FE_State &amp; _fe);
  
  /*
   * Deep copying support
   * --------------------
   * This copy constructor and operator= are defined to make a
   * deep copy, making it possible to pass/return vectors by value
   * and assign from one vector to another. The entire contents of
   * the vector, including all elements, are copied. Each vector
   * element is copied from the original vector to the copy using
   * assignment (operator=). Making copies is generally avoided
   * because of the expense and thus, vectors are typically passed
   * by reference, however, when a copy is needed, these operations
   * are supported.
   */
      const Vector &amp; operator=(const Vector &amp; rhs);
      Vector(const Vector &amp; rhs);
  
  private:
      ElemType *elements;
      int numAllocated, numUsed;
      long timestamp;
  
      void checkRange(int index, const char *msg);
      void enlargeCapacity();
      void copyInternalData(const Vector &amp; other);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>/*
   * File: vector.h
   * Last modified on Fri Jun  5 15:35:35 2009 by eroberts
   *      modified on Tue Jan  2 13:56:15 2007 by zelenski
   * -----------------------------------------------------
   * This interface file contains the Vector class template, an
   * efficient, safer, convenient replacement for the built-in array.
   */
  
  #ifndef _vector_h
  #define _vector_h
  
  #include "genlib.h"
  #include "strutils.h"
  #include "foreach.h"
  
  /*
   * Class: Vector
   * -------------
   * This interface defines a class template that stores a homogeneous
   * indexed collection. The basic operations are similar to those
   * in the built-in array type, with the added features of dynamic
   * memory management, bounds-checking on indexes, and convenient
   * insert/remove operations. Like an array, but better!
   * For maximum generality, the Vector is supplied as a class template.
   * The client specializes the vector to hold values of a specific
   * type, e.g. Vector&lt;int&gt; or Vector&lt;studentT&gt;, as needed
   */
  
  template &lt;typename ElemType&gt;
  class Vector {
  
  public:
  
  /* Forward references */
      class Iterator;
  
  /*
   * Constructor: Vector
   * Usage: Vector&lt;int&gt; vec;
   *        Vector&lt;student&gt; dormlist(200);
   *        Vector&lt;string&gt; *vp = new Vector&lt;string&gt;;
   * -----------------------------------------------
   * The constructor initializes a new empty vector. The optional
   * argument is a hint about the expected number of elements that
   * this vector will hold, which allows vector to configure itself
   * for that capacity during initialization.  If not specified,
   * it is initialized with default capacity and grows as elements
   * are added. Note that capacity does NOT mean size, a newly
   * constructed vector always has size() = 0. A large starting
   * capacity allows you to add that many elements without requiring
   * any internal reallocation. The explicit keyword is required to
   * avoid accidental construction of a vector from an int.
   */
      explicit Vector(int sizeHint = 0);
  
  /*
   * Destructor: ~Vector
   * Usage: delete vp;
   * -----------------
   * The destructor deallocates storage associated with this vector.
   */
      ~Vector();
  
  /*
   * Method: size
   * Usage: nElems = vec.size();
   * ---------------------------
   * This method returns the number of elements in
   * this vector.
   */
      int size();
  
  /*
   * Method: isEmpty
   * Usage: if (vec.isEmpty())...
   * -----------------------------
   * This method returns true if this vector contains no
   * elements, false otherwise.
   */
      bool isEmpty();
  
  /*
   * Method: getAt
   * Usage: val = vec.getAt(3);
   * --------------------------
   * This method returns the element at the specified index
   * in this vector. Elements are indexed starting from 0.  A call to
   * vec.getAt(0) returns the first element, vec.getAt(vec.size()-1)
   * returns the last. Raises an error if index is outside the range
   * [0, size()-1].
   */
      ElemType getAt(int index);
  
  /*
   * Method: setAt
   * Usage: vec.setAt(3, value);
   * ---------------------------
   * This method replaces the element at the specified index
   * in this vector with a new value.  The previous value at that
   * index is overwritten with the new value. The size of the vector
   * is unchanged. Raises an error if index is not within the
   * range [0, size()-1].
   */
      void setAt(int index, ElemType value);
  
  /*
   * Method: operator[]
   * Usage: vec[0] = vec[1];
   * -----------------------
   * This method overloads [] to access elements from
   * this vector. This allows the client to use array-like notation
   * to get/set individual vector elements. Returns a reference to
   * the element to allow in-place modification of values. Raises
   * an error if index is not within the range [0, size()-1].
   */
      ElemType &amp; operator[](int index);
  
  /*
   * Method: add
   * Usage: vec.add(value);
   * ----------------------
   * This method adds an element to the end of this vector.
   * The vector's size increases by one.
   */
      void add(ElemType elem);
  
  /*
   * Method: insertAt
   * Usage: vec.insertAt(0, value);
   * ------------------------------
   * This method inserts the element into this vector at
   * the specified index, shifting all subsequent elements one
   * index higher. A call to vec.insertAt(0, val) inserts a new
   * element at the beginning, vec.insertAt(vec.size(), val) add
   * a new element to the end. The vector's size increases by one.
   * Raises an error if index is outside the range [0, size()].
   */
      void insertAt(int index, ElemType elem);
  
  /*
   * Method: removeAt
   * Usage: vec.removeAt(3);
   * -----------------------
   * This method removes the element at the specified
   * index from this vector, shifting all subsequent elements one
   * index lower. A call to vec.removeAt(0) removes the first
   * element, vec.removeAt(vec.size()-1), removes the last. The
   * vector's size decreases by one. Raises an error if index is
   * outside the range [0, size()-1].
   */
      void removeAt(int index);
  
  /*
   * Method: clear
   * Usage: vec.clear();
   * -------------------
   * This method removes all elements from this vector. The
   * vector is made empty and will have size() = 0.
   */
      void clear();
  
  /*
   * SPECIAL NOTE: mapping/iteration support
   * ---------------------------------------
   * The Vector class supports both a mapping operation and an iterator which
   * allow the client access to all elements one by one.  In general,
   * these  are intended for _viewing_ elements and can behave
   * unpredictably if you attempt to modify the vector's contents during
   * mapping/iteration.
   */
  
  /*
   * Method: mapAll
   * Usage: vector.mapAll(Print);
   * ----------------------------
   * This method iterates through this vector's contents
   * and calls the function fn once for each element.
   */
      void mapAll(void (*fn)(ElemType elem));
  
  /*
   * Method: mapAll
   * Usage: vector.mapAll(PrintToFile, outputStream);
   * ------------------------------------------------
   * This method iterates through this vector's contents
   * and calls the function fn once for each element, passing
   * the element and the client's data. That data can be of whatever
   * type is needed for the client's callback.
   */
      template &lt;typename ClientDataType&gt;
      void mapAll(void (*fn)(ElemType elem, ClientDataType &amp; data),
                  ClientDataType &amp; data);
  
  /*
   * Method: iterator
   * Usage: iter = vector.iterator();
   * --------------------------------
   * This method creates an iterator that allows the client to
   * iterate through the elements in this vector.  The elements are
   * returned in index order.
   *
   * The idiomatic code for accessing elements using an iterator is
   * to create the iterator from the collection and then enter a loop
   * that calls next() while hasNext() is true, like this:
   *
   *     Vector&lt;int&gt;::Iterator iter = vector.iterator();
   *     while (iter.hasNext()) {
   *         int elem = iter.next();
   *         . . .
   *     }
   *
   * This pattern can be abbreviated to the following more readable form:
   *
   *     foreach (int elem in vector) {
   *         . . .
   *     }
   *
   * To avoid exposing the details of the class, the definition of the
   * Iterator class itself appears in the private/vector.h file.
   */
      Iterator iterator();
  
  private:
  
  #include "private/vector.h"
  
  };
  
  #include "private/vector.cpp"
  
  #endif
  </code></pre>