使用模板类复制成员函数

时间:2013-10-31 03:23:32

标签: c++

我的会员功能遇到了问题。

我的目标是创建我的集合的副本,并返回指向它的指针。

            template <class T>
            class Set
            {
            public:
                Set(int length = 0);     //Default constructor
                ~Set();              //Defualt Destructor
                int size();          //Return how many elements are in set
                bool contains(T test);   //Searches set for T
                bool add(T adding);      //Adds T to set, repeats are denied
                bool remove(T removing); //Attempts to remove T
                T** elements();      //Returns a pointer to the set
                T** copy();          //Creates a copy of the set, and returns a pointer to it
                T &operator[](int sub);  //Overload subscript

            private:
                T** set;        //Pointer to first of set
                int setSize;        //Int holding amount of Elements available
                int holding;        //Elements used
                void subError();    //Handles Subscript out of range
                void adder();       //returns a copy with +1 size
            };

这是我的构造函数和复制函数:

            template <class T>
            Set<T>::Set(int length) //Default constructor
            {
                for(int i = 0; i < length; i++)
                {
                    set[i] = new T;
                }
                setSize = length;
                holding = 0;
            }

            template <class T>
            T** Set<T>::copy()  //Creates a copy of the set, and returns a pointer to it
            {
                T** setCopy;
                for(int i = 0; i < setSize; i++)
                {
                    setCopy[i] = new T;
                    *setCopy[i] = *set[i];
                }
                return setCopy;
            }

有错误我得到错误错误C4700:未初始化的局部变量'setCopy'使用 和C4700:使用未初始化的局部变量'temp' 我已经尝试过各种各样的去除力的方式,但是我无处可去。

1 个答案:

答案 0 :(得分:0)

setCopy实际上是一个未初始化的...想想 在T** setCopy;之后 它指向哪里?

相关问题