2

我的 DynamicArray.cpp 类中出现此错误:“错误 C2143:语法错误:缺少 ';' 在“模板”之前”DynamicArray.cpp 的第三行。这仍然是一项正在进行的工作,但我也非常感谢对设计的批评。谢谢 DynamicArray.h

    #ifndef DYNAMICARRAY_H
    #define DYNAMICARRAY_H
    using namespace std;

    template <class T>
    class DynamicArray
    {
     private:
       T *origin;
       T *allocator;
       int size;
       int current;


     public:
        DynamicArray();
        DynamicArray(int);
        DynamicArray(const DynamicArray&);
        ~DynamicArray();
        void add(int);
        void erase(int);
        void empty();
        T at(int);
        void put(T);
        void remove();
        DynamicArray operator=(const DynamicArray&);
        int getSize();
        void operator[](int);
        T operator=(const T&);
        void insert(T&);
    }


    #endif // !DYNAMICARRAY_H

动态数组.cpp

    #include "DynamicArray.h"

    template <class T>
    DynamicArray<T>::DynamicArray()
    {
       origin = new T[5];
       allocator = NULL;
       size = 5;
       current = 0;
       for(int counter = 0; counter <= size - 1; counter++)
       {
         *origin[counter] = NULL;
       }
    }

     template <class T>
     DynamicArray<T>::DynamicArray(int size)
     {
     this->size = size;
     origin = new T[size];

     for(int counter = 0; counter <= size - 1; counter++)
     {
      *origin[counter] = NULL;
     }
     }

     template <class T>
     DynamicArray<T>::DynamicArray(const DynamicArray& obj)
     {
       empty();
       for(int counter = 0; counter < obj.size - 1; counter++)
       {
      origin[counter] = obj.origin[counter];
       }
     }

     template <class T>
     DynamicArray<T>::~DynamicArray()
     {
      delete [] origin;
      delete [] allocator;
      size = NULL;
     }

     template <class T>
     void DynamicArray<T>::add(int size)
     {
      allocator = new T[size]
      for(int counter = 0; counter < this-> size - 1; counter++)
      {
      allocator[counter] = *origin[counter];
      }
      origin = NULL;
      origin = new T[size];
      this->size = size;

      for(int counter = 0; counter < size - 1; counter++)
      {
      *origin[counter] = allocator[counter];
      }
      allocator = NULL;
    }

      template <class T>
      void DynamicArray<T>::erase(int ammount)
      {
      if(ammount - size > size)
      throw "\nnegetive memory location error\n";

      allocator = new T[size - ammount];

      for(int counter = 0; counter < this-> size - ammount - 1; counter++)
      {
      *allocator[counter] = origin[counter];
      }

      origin = NULL;
      size = size - ammount;
      origin = new T[size];

      for(int counter = 0; counter < size - 1; counter++)
      {
      origin[counter] = allocator[counter];
      }
      allocator = NULL;
    }

    template <class T>
    void DynamicArray<T>::empty()
    {
      origin = NULL;
      allocator = NULL;
    }

    template <class T>
    T DynamicArray<T>::at(int location)
    {
      if(location > size || location < 0)
      {
      throw "\nMemory Location does not exist\n";
      }
      else
      {
      current = location;
      return *origin[location];
      }
      }

    /*template <class T>
    void DynamicArray::put(T item)
    {
      add(1);
      origin[size - 1] = item;
    }*/

    template <class T>
    void DynamicArray<T>::remove()
    {
      erase(1);
    }

    template <class T>
    DynamicArray<T> DynamicArray<T>::operator=(const DynamicArray& obj)
    {
      empty();
      for(int counter = 0; counter < obj.size - 1; counter++)
      {
      origin[counter] = obj.origin[counter];
      }

      return *this;
    }

    template <class T>
    int DynamicArray<T>::getSize()
    {
      return size;
    }

    template <class T>
    void DynamicArray<T>::operator[](int index)
    {
      at(index);
    }

    template <class T>
    T DynamicArray<T>::operator=(const T &value)
    {
      put(value);
      return *this;
    }

    template <class T>
    void DynamicArray<T>::insert(T &value)
    {
      int counter = 0;
      do
      {
      if(*origin[counter] == NULL)
      {
          origin[counter] = value;
      }
      counter++
      }
      while(*origin[counter] != NULL)
    }        
4

2 回答 2

1

你需要;在你的课结束时:

    T operator=(const T&);
    void insert(T&);
};


#endif // !DYNAMICARRAY_H

此外,您需要在头文件中定义模板化函数。模板的工作方式定义需要可用于调用函数或访问所涉及数据的任何代码。编译器为使用的每种模板创建一个新的函数实例。此实例化在使用模板化数据/函数的代码中完成。

于 2012-10-05T05:57:55.663 回答
1

There are two problems. First, you need a ; at the end of your class declaration. Once that is fixed, you will run into problems because your template method definitions should also be in the header file, or in a file that is accessible to the code that tries to instantiate your templates.

于 2012-10-05T06:00:11.987 回答