Whats going on? Here is the functions it references. I am trying to get this to work as a copy constructor
template <class T>
const queue<Base>& queue<T>::operator=(const queue<Base> &q){
// Doesn't need to copy if they are the same object
if (this != &q){
delete [] data;
length = q.length;
capacity = q.capacity;
front = q.front;
data = new T[capacity];
for (int i = 0; i < capacity; i++){
data[i] = q.data[i];
}
}
return this;
}
This is your error
return this;
this is a pointer. Your operator = is declared as returning a reference. A pointer cannot be converted to a reference. This is what the error message is telling you.
Related
I am playing around with my containers.
In my template class for an container i make a pointer of T* (T* mData;) as a member variable of the container class. This is a pointer to an array type.
In the destructor i call the delete keyword delete mData; or delete[] mData; but all i hear is a ding in the test application and no more output is write to the console.
Can someone please explain what is going on is deleting a member of T* mData; somehow different to deleting everything else? if so can someone please explain I've looked everywhere and can't find an answer.
UPDATE
I read the comments and since people were willing to help in return i have decided to post the this update and answer the question itself. Ps don't laugh it was a silly mistake.
OLD CODE (looked something like this)
template<typename T>
class TestVector {
// members
T* mData;
size_t mSize;
// methods
void AddData(T data){
T* buffer;
memcpy_s(buffer, sizeof(mData), mData, sizeof(mData));
if(!buffer){
// log error
return;
}
mSize++;
SafeDelete(mData); // deleting this here was the problem (check destructor)
mData = new T[mSize];
mData = buffer;
mData[mSize - 1] = data;
}
// constructors
TestVector(void){
mData = new T[0];
ZeroMemory(mData, sizeof(mData);
mSize = 0;
}
~TestVector(void){
SafeDelete(mData); // when deleting the mData member again here the problem would occur, the destructor was called the beep noise would sound and the console window would freeze
}
// operators
};
I will answer what i did to change this as a Answer to the question . . .
I will admit i do not understand why deleting the same pointer of the class more than once gave an error, maybe the memory address of the new pointer wasn't know to the class so the destructor couldn't find it but to solve the issue i went with old school C.
template<typename T>
class TestVector {
private:
T * mData;
size_t mSize = 0;
public:
TestVector(void) {
mSize = 2;
mData = (T*)malloc(mSize * sizeof(T));
for (int i = 0; i < mSize; i++) {
mData[i] = i;
}
cout << "Values in t1 are: " << mData[0] << ", " << mData[1] << endl;
}
~TestVector(void) {
free(mData); // only place the pointer is ever deleted
}
public:
void AddData(T data) {
mSize++;
T* newData = (T*)realloc(mData, mSize * sizeof(T));
mData = newData;
mData[mSize - 1] = data;
for (int i = 0; i < mSize; i++) {
cout << "Value: " << i << " is equal to: " << mData[i] << endl;
}
}
};
I just used malloc() to allocate, realloc() to reallocate a bigger array to the pointer and free() to release the pointer.
I could of just omitted the SafeDelete(mData); in the first version and just called new over the top i think but i actually prefer using old school C for this type of thing, it makes more sense :)
i have a little problem with my code in c++.. I need to doit this way, because it's work to school..
I have template named Catalog
template <typename T>
class Catalog
{
struct Item{
T* _product;
unsigned int _amount;
Item* _next = nullptr;
};
Item* _head;
Item* _actual;
Item* _last;
int _size;
void init()
{
this->_size = 0;
this->_head = nullptr;
this->_actual = nullptr;
};
public:
Catalog(void)
{
this->init();
};
T*& operator[](unsigned int){
Item* node = this->_head;
for (int i = 0; i < this->_size; i++)
{
if (i == pos)
{
return &node->_product;
}
node = node->_next;
}
return nullptr;
};
};
It's a structure where i have Items and in Items i have pointer to next one in array..
I tryed to like this
Catalog<Products> *catalog = new Catalog<Products>();
Products *pr1 = new ProductA(5, "jmeno", 5);
catalog->Add(pr1, 5);
Products* ct = catalog[0];
In my case visual studio is reporting this error
IntelliSense: no suitable conversion function from SemestralniPrace::Catalog<Products>" to "Products *" exists
I want to correct operator[] so i can use my catalog like i need to.. or correct the code in main..
It's for example, i have some more functions in class Catalog, but it isn't importnent for this problem..
Can someone help me please.. Even suggestions is good for me. I am desperate with this.
Thanks and sorry for my English, it's not mine native language.
You have two mistakes. The first one is relative to the return statement of the operator []. The type of expression &node->_product is T** while the return type of the operator is T*&
You have to write simply
return node->_product;
The second mistake is relative to statement
Products* ct = catalog[0];
You have to write either
Products* ct = ( *catalog )[0];
or
Products* ct = catalog[0][0];
I seem to be having issues in a project trying to create a pointer to "this" where "this" is the first LinkedList in a list in C++. The first object has data in it, second does...etc until the this->m_next is NULL
The compiler is spitting this out at me:
linkedlist.hpp:55:22: error: invalid conversion from âconst LinkedList<int>* constâ to âLinkedList<int>*â [-fpermissive]
What am I doing wrong?
template <typename T>
int LinkedList<T>::size() const
{
int count = 0;
LinkedList* list = this; // this line is what the compiler is complaining about
//adds to the counter if there is another object in list
while(list->m_next != NULL)
{
count++;
list = list->m_next;
}
return count;
}
The member function is marked const. Which means that this is const as well. You need to do:
const LinkedList<T>* list = this; // since "this" is const, list should be too
// ^
// |
// Also added the template parameter, which you need, since "this"
// is a LinkedList<T>
Change
LinkedList* list = this;
to
const LinkedList<T>* list = this;
^^^^^ ^^^
Since your function is defined as const, the this pointer is automatically of type const LinkedList<T>*
Thus you cannot assign a const pointer to a non const pointer, explaining the error.
The missing <T> would likely give you errors if you try non int parameters.
Try changing
LinkedList* list = this; // this line is what the compiler is complaining about
to
LinkedList<T> const * list = this;
^^^ ^^^^^
I want to deep copy an array of int. I get an Assertion Error: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) when it goes through the destructor. Which I've been told is because It's trying to delete something that is not there. Please let me know if I am on the right track and just need to change something small, or if I am completely lost and don't know it. I can add more code if needed.
Thanks for the answers.
.h
private:
int* _myArray;
int _size;
int _capacity;
.cpp
MyVector::MyVector()
{
_myArray = new int[2];
_size = 0;
_capacity = 2;
}
MyVector::MyVector(int aSize)
{
_myArray = new int[aSize];
_size = 0;
_capacity = aSize;
}
MyVector::~MyVector()
{
if(_myArray != NULL)
{
delete[] _myArray;
_myArray = NULL;
}
}
MyVector::MyVector(const MyVector& mVector)
{
_capacity = mVector._capacity;
_size = mVector._size;
// if(mVector._myArray)
// {
// _myArray = new int[_capacity];
// copy(mVector._myArray, mVector._myArray+_capacity, _myArray);
// }
}
MyVector& MyVector::operator=(MyVector& setterVect)
{
delete [] _myArray;
if(setterVect._myArray)
{
_myArray = new int[_capacity];
copy(setterVect._myArray, setterVect._myArray+_capacity, _myArray);
}
return *this;
}
You need to make sure you are following the "Rule of Three".
Apart from copy constructor & destructor You should also provide a copy assignment operator which should do a deep copy of dynamically allocated pointer member.
On a side note, the best solution is to simply drop the dynamically allocated member and use a std::vector it saves you all the hassles of manual memory management.
I wrote this function in C++ as part of a bigger program:
Object Single_list<Object>::pop_front() {
//Single_node<Object> *tmp_front;
//Object hold;
if (empty()) {
throw underflow();
}
Single_node<Object> first_node = front();
Single_node<Object> *ptr = list_head;
list_head = list_head->next();
delete ptr;
return first_node.retrieve();
}
However, when I try and use this function I get the following message:
WARNING: calling delete twice on the same memory location: 0x100100160
I am really confused, I am not deleting the pointer (assuming that's what is causing the problem?) twice.
Any advice will be appreciated.
As requested, here is the constructor and retrieve function for the Single_Node, although I am not sure how helpful this will be to the error regarding deletion that I am getting:
template <typename Object>
Single_node<Object>::Single_node( const Object &e, Single_node<Object> *n ):element( e ), next_node( n )
{
// empty constructor
}
template <typename Object>
Object Single_node<Object>::retrieve() const
{
return element;
}
template <typename Object>
Single_node<Object> *Single_node<Object>::next() const
{
return next_node;
}
Building on Mankarse's comment, try the following:
Object Single_list<Object>::pop_front() {
if (empty()) {
throw underflow();
}
Object result = front().retrieve();
Single_node<Object> *ptr = list_head;
list_head = list_head->next();
delete ptr;
return result;
}
This grabs the result from the first node before it gets deleted.