I have this class:
class HIDValue{
private:
void* mValue;
UINT mSize;
HIDElement mElement;
public:
HIDValue() {
mValue = 0;
mSize = 0;
}
template <class T>
HIDValue(T pValue, HIDElement pElement) {
mElement = pElement;
mValue = 0;
setValue(pValue);
}
HIDValue(const HIDValue& pValue) {
mSize = pValue.mSize;
mElement = pValue.mElement;
mValue = 0;
if(mSize) {
mValue = new char[mSize];
memcpy(mValue, pValue.mValue, mSize);
}
}
template <class T>
void setValue(T pValue) {
if(mValue)
delete mValue;
mValue = new T;
*((T*)mValue) = *((T*)&pValue);
mSize = sizeof(T);
}
~HIDValue() {
//THE MENTIONED ERROR IS HERE
if(mValue)
delete mValue;
}
void setElement(HIDElement pElement) {
mElement = pElement;
}
const HIDElement& getElement() const {
return mElement;
}
template <class T>
bool getValue(T* pValue) const {
if(mValue && mSize <= sizeof(T)) {
*pValue = *((T*)mValue);
return true;
}
return false;
}
};
With some changes it’s working fine:
class HIDValue{
private:
//void* mValue;
char mValue[16];
UINT mSize;
HIDElement mElement;
public:
HIDValue() {
//mValue = 0;
mSize = 0;
}
template <class T>
HIDValue(T pValue, HIDElement pElement) {
mElement = pElement;
//mValue = 0;
setValue(pValue);
}
HIDValue(const HIDValue& pValue) {
mSize = pValue.mSize;
mElement = pValue.mElement;
//mValue = 0;
if(mSize) {
//mValue = new char[mSize];
memcpy(mValue, pValue.mValue, mSize);
}
}
template <class T>
void setValue(T pValue) {
//if(mValue)
// delete mValue;
//mValue = new T;
*((T*)mValue) = *((T*)&pValue);
mSize = sizeof(T);
}
~HIDValue() {
//itten egy hiba vala
//if(mValue)
// delete mValue;
}
void setElement(HIDElement pElement) {
mElement = pElement;
}
const HIDElement& getElement() const {
return mElement;
}
template <class T>
bool getValue(T* pValue) const {
if(mValue && mSize <= sizeof(T)) {
*pValue = *((T*)mValue);
return true;
}
return false;
}
};
I am curious what is the reason of the error.
Thanks ahead and sorry for my english!
void*. You want to use Boost.Any or Boost.Variant (latter if the set of allowed types is to be restricted).memcpy. It’s likely to not work correctly with UDTs.The error is most likely related to the fact that you’re deleting
void*instead ofT*.