argument has type const but function is not marked const
Aug 30, 2016 at 12:09pm UTC
I am getting the error
"error: member function 'listSize' not viable: 'this' argument has type 'const LinkedList', but function is not marked const
if(lane.listSize() == 0){"
I am trying to write a function that tells if a linked list is empty.
first i initialise the linked list in the constructor
LinkedList lane;
below the function to check if lane is empty
1 2 3 4 5 6 7 8
bool List::empty() const {
if (lane.listSize() == 0){
return true ;
}
else {
return false ;
}
}
it is throwing the error at the listSize() function from my LinkedList.cpp which is as follows.
1 2 3
int LinkedList::listSize() {
return count;
}
from the error message i can tell that my problem is my code is not const correct however im unsure how to fix it.
P.S. sorry for poor formatting my internet is too slow to get the toolbar working properly.
Last edited on Aug 30, 2016 at 12:10pm UTC
Aug 30, 2016 at 12:11pm UTC
Add const to listSize():
1 2
int LinkedList::listSize() const {
return count;
Aug 30, 2016 at 12:43pm UTC
thanks that worked a charm
Topic archived. No new replies allowed.