I am struggling to write a copy constructor for my linked list class.
I keep getting the following error:
LinkedList.cpp: In copy constructor âLinkedList::LinkedList(const LinkedList&)â:
LinkedList.cpp:22:23: error: passing âconst LinkedListâ as âthisâ argument of âNode* LinkedList::getHead()â discards qualifiers [-fpermissive]
I never made a "deep" copy constructor before, and I really have no idea what I'm doing.
"Discards qualifier" usually means you called a non-read only member function. Your getHead function does not guarantee that the variables in the const variable List are not modified, thus breaking the const attribute of the variable.
Just make your getHead function read-only if it does not modify any variables.
1 2 3 4 5 6 7
//Function prototype
getHead()const; //const after the parameter parentheses makes the function read-only
//i.e. the values can be retrieved but not modified
//Function definition
LinkedList::getHead()const{
/*...*/
}