У меня есть проблема, которая сводит меня с ума:
У меня есть функция-член:
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 *'
РЕДАКТИРОВАТЬ:
Ошибка в "return entity.append (& e);"line.
entities
равно DynamicArray<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";
}
}
(программа вылетает из-за нарушения доступа к памяти, кстати).