#include <iostream>
using namespace std;
template <typename T>
class SmartPointer
{
T *ptr;
int numElem; //-1 if an element. >=0 if an array
public:
SmartPointer(T pNum);
SmartPointer();
SmartPointer(T *pArray, int pSize);
~SmartPointer();
SmartPointer(std::initializer_list<T> nums, int pSize); //ERROR HERE (line 33)
T getValue() const;
T getValue(int index) const;
void setValue(T pNum);
void setValue(T pNum, int index);
int getNumElem() const;
SmartPointer<T> operator+ (const SmartPointer<T>& ptr);
SmartPointer<T> operator- (const SmartPointer<T>& ptr);
SmartPointer<T> operator* (const SmartPointer<T>& ptr);
};
template <class T>
SmartPointer<T>::SmartPointer(std::initializer_list<T> nums, int pSize) //line (45)
{
ptr = new T[pSize];
std::initializer_list<T>::iterator it;
int i = 0;
for (it = nums.begin() ; it != nums.end() ; ++it)
{
ptr[i] = *it;
i++;
}
}
(The rest of the code...)
我正在尝试实现一个简单的智能指针,如果我尝试编译代码,我会得到两个错误:
1)第33行:预期')'在'&lt;'之前令牌
2)第45行:在'('token
之前的预期构造函数,析构函数或类型转换但我无法确定这些错误的原因。我想要的是我想将intializer_list传递给构造函数,以便它在内部(在类内)动态分配内存,如下所示:
SmartPointer<int> test({1,2,3},3)
//do something with test