访问数组中的元素c ++

时间:2018-02-25 04:59:01

标签: c++ arrays templates pointers

我正在使用模板化的类并创建一个数组来存储值。我需要重载此数组中的[]运算符,这就是我遇到问题的地方。

template <class TYPE>
class ArrayList : public ListInterface<TYPE>
{
public:
    TYPE * aL = new TYPE[25];
}

对于我为[]重载的operater,我有以下内容:

    TYPE & operator[] (int num) const  //will addd throw std out of range
{
    return aL[num];
}

但是,当我稍后在程序中使用此运算符时,它不会返回该特定索引处的值。

void insertAt(int index, const TYPE & newEntry)
    //throw (std::out_of_range)...
{
    aL[index] = newEntry;
    TYPE a = aL[index]; //use this to see what it is returning
}

我使用变量&#34; a&#34;作为一个调试工具来查看为aL [index]返回的内容,我得到了&#34; a&#34; :-858993460。我确信这一定是一个简单的错误,我已经搜索了很长时间才弄清楚为什么会发生这种行为但是却无法找到它的底部。谢谢你的帮助!

0 个答案:

没有答案