类型'*'的表达式对类型'&'的引用的初始化无效

时间:2019-04-01 20:24:58

标签: c++ templates reference initialization

我有一个让我发疯的问题:

我有一个成员函数:

template <class T, unsigned char _size>
T& DynamicArray<T, _size>::append(const T& newE)
{
    if((dedicated - (length + 1) == 0))
    {
        transfer(length + 1);
        elements[length - 1] = newE;
        return elements[length - 1] = newE;
    } else return (elements[length++] = newE);
}

然后,我这样称呼它:

grphd::graphic::Entity& grphd::graphic::Grid::addEntity(graphic::Entity &e)
{
    try
    {
        return entities.append(&e); //RVALUE = ENTITY*
                                    //LVALUE = ENTITY& ?! --> ENTITY*&!
    } catch(std::exception &e)
    {
        std::cout << "Exception on 'addEntity'.\n";
    }

}

它给了我这个错误:

  

错误:类型的引用的无效初始化   类型表达式中的“ grphd :: graphic :: Entity&”   'grphd :: graphic :: Entity *'

编辑:

错误出现在“返回entities.append(&e);”处线。

entitiesDynamicArray<Entity*, 1> entities;

DynamicArray是:

#ifndef DYN_ARR_H_INCLUDED
#define DYN_ARR_H_INCLUDED

template <class T, unsigned char _size>
class DynamicArray
{
    T* elements; //DONE
    unsigned char length; //DONE
    unsigned char dedicated; //DONE
    static unsigned char minDiffer; //DONE

    T& transfer(unsigned char); //DONE

    public:

    DynamicArray(); // DONE

    unsigned char getDedicated() const {return dedicated;} //DONE
    unsigned char getLength() const {return length;} //DONE
    void* getElementsPtr() const {return reinterpret_cast<void*>(elements);} //DONE

    T& append(const T&); //DONE
    T& insert(const T&, unsigned char); //DONE
    bool kickoutLast(); //DONE
    bool remove(unsigned char); //DONE

    T& operator[] (unsigned char); //DONE
    T operator[] (unsigned char) const;
};

template <class T, unsigned char _size>
void writeDynamicArray(const DynamicArray <T, _size>& da);

#include "dyn_arr.cpp"

#endif // DYN_ARR_H_INCLUDED

---和---

template <class T, unsigned char _size>
DynamicArray<T, _size>::DynamicArray():
    length(_size),
    dedicated(_size + minDiffer)
{
    try
    {
        elements = new T[dedicated];
    } catch(std::exception &e)
    {
        std::cout << "Error on allocation \"elements = new T[dedicated]\".\n";
    }
}

template <class T, unsigned char _size>
T& DynamicArray<T, _size>::transfer(unsigned char len)
{
    T* tmpPtr;
    try
    {
        tmpPtr = new T[len + minDiffer];
    } catch(std::exception &e)
    {
        std::cout << "Error on allocation \"T* tmpPtr = new T[len + minDiffer]\".\n";
    }

    for(
        unsigned char i = 0;
        i < length;
        tmpPtr[i] = elements[i], i++
       );
    delete [] elements, elements = tmpPtr,
    length = len, dedicated = len + minDiffer;
}

template <class T, unsigned char _size>
T& DynamicArray<T, _size>::append(const T& newE)
{
    if((dedicated - (length + 1) == 0))
    {
        transfer(length + 1);
        elements[length - 1] = newE;
        return elements[length - 1] = newE;
    } else return (elements[length++] = newE);
}

template <class T, unsigned char _size>
bool DynamicArray<T, _size>::kickoutLast()
{
    if((length - 1) != 0)
    {
        delete (elements + dedicated - 1);
        dedicated--;
        length--;
        return 1;
    } return 0;

}

template <class T, unsigned char _size>
bool DynamicArray<T, _size>::remove(unsigned char n)
{
    if(n >= (length - 1)) return false;

    kickoutLast();

    for(unsigned char i = n; i < (length); i++)
    {
        elements[i] = elements[i + 1];
    } return true;
}

template <class T, unsigned char _size>
T& DynamicArray<T, _size>::insert(const T& newE, unsigned char n)
{
    if((dedicated - (length + 1) == 0)) transfer(length + 1);

    for(short i = length; i > n; i--)
    {
        elements[i] = elements[i - 1];
    }

    elements[n] = newE;
    length++;
}

template <class T, unsigned char _size>
T& DynamicArray<T, _size>::operator[] (unsigned char n)
{
    return elements[n];
}

template <class T, unsigned char _size>
T DynamicArray<T, _size>::operator[] (unsigned char n) const
{
    return elements[n];

}

template <class T, unsigned char _size>
void writeDynamicArray(const DynamicArray <T, _size>& da)
{
    for(unsigned char i = 0; i < da.getLength(); i++)
        std::cout << da[i] << '\n';
}

template <class T, unsigned char _size>
unsigned char DynamicArray<T, _size>::minDiffer = 8;

修复:

grphd::graphic::Entity*& grphd::graphic::Grid::addEntity(graphic::Entity &e)
{
    try
    {
        return entities.append(&e); //RVALUE = ENTITY*
                                    //LVALUE = ENTITY& ?! --> ENTITY*&!
    } catch(std::exception &e)
    {
        std::cout << "Exception on 'addEntity'.\n";
    }

}

(该程序由于内存访问冲突而崩溃,顺便说一句)。

0 个答案:

没有答案