I am trying to save an instance of my custom class to a vector, but I get the following error during compilation:
error: binding 'const anil::cursor_list' to reference of type 'anil::cursor_list&' discards qualifiers
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
Could you help me figure what I'm doing wrong?
The snippet of code that triggers this is as follows:
std::vector<anil::cursor_list> strongly_connected_components;
anil::cursor_list strongly_connected_component_list;
strongly_connected_components.push_back(strongly_connected_component_list);
The declaration for this class is as follows:
#include <cstddef>
#include <iostream>
namespace anil {
class cursor_list_node {
private:
int data;
cursor_list_node* next;
cursor_list_node* previous;
friend class cursor_list;
};
class cursor_list {
private:
// Data:
int m_index;
int m_size;
cursor_list_node* front;
cursor_list_node* back;
cursor_list_node* cursor;
int m_backup_index;
cursor_list_node* backup_cursor;
// Functions:
void delete_list();
public:
cursor_list() : m_index(-1), m_size(0), front(nullptr), back(nullptr),
cursor(nullptr), m_backup_index(-1), backup_cursor(nullptr) {}
cursor_list(cursor_list& copied_list);
bool is_empty();
int size();
int index();
int front_data();
int back_data();
int cursor_data();
bool operator==(cursor_list& rhs); // rhs = right hand side
cursor_list& operator= (cursor_list& rhs); // rhs = right hand side
friend std::ostream& operator<<(std::ostream& out, cursor_list& rhs); // rhs = right hand side
void clear();
void move_cursor_front();
void move_cursor_back();
void move_cursor_prev();
void move_cursor_next();
void save_cursor_state();
void restore_cursor_state();
void prepend(int new_data);
void append(int new_data);
void insert_before_cursor(int new_data);
void insert_after_cursor(int new_data);
void delete_front();
void delete_back();
void delete_cursor();
~cursor_list();
};
}
Lastly, the constructors for the cursor_list
class are as follows:
cursor_list() : m_index(-1), m_size(0), front(nullptr), back(nullptr),
cursor(nullptr), m_backup_index(-1), backup_cursor(nullptr) {}
anil::cursor_list::cursor_list(cursor_list& copied_cursor_list) {
this->m_index = -1;
this->m_size = 0;
this->front = nullptr;
this->back = nullptr;
this->cursor = nullptr;
this->m_backup_index = -1;
this->backup_cursor = nullptr;
if (copied_cursor_list.is_empty() == false) {
for (cursor_list_node* it = copied_cursor_list.front; it != nullptr;
it = it->next) {
this->append(it->data);
}
}
}
Edit:
By following prog-fh's explanations and suggestions, I was able to solve the problem described above.
I achieved this by changing the parameter for the copy constructor from cursor_list& copied_cursor_list
to const cursor_list& copied_cursor_list
and changing the pointer that I used to copy the values of the list from cursor_list_node* it
to const cursor_list_node* it
.