Howdy. I have a doubly linked list class that I was given the function names for. One of them is called CopyAll which should copy all the nodes from the input doubly linked list into the base list. When trying to run it in another program, however, an error was returned saying I could not access the private member declared in the class. Here is the function as I've written it in the header:
1 2 3 4 5 6 7 8 9
template <typename T>
void Dlist<T>::CopyAll(const Dlist &l) {
node* l_node = l.first;
while (l_node != NULL) { //iterate through the list to copy from
T l_o = l_node->o;
InsertBack(l_o); //pop the node to copy to the back of the list
l_node = l_node->next;
}
}
I had an epiphany writing this and rewrote the funcion as so:
1 2 3 4 5 6 7
template <typename T>
void Dlist<T>::CopyAll(const Dlist &l) {
while (!l.IsEmpty()) {
node* current = l.RemoveBack();
InsertFront(current);
}
}
Unfortunately, I ran into two problems. Firstly, Visual Studio suggests function names when applicable and it wasn't suggesting anything other than private members defined in the class, so I'm not sure if l.RemoveBack() is a functional line in the header. Secondly, the same error message is still showing in my main file:
Error C2248 'Dlist<double>::CopyAll': cannot access private member declared in class 'Dlist<double>'
Can anyone explain to me what I'm getting wrong here? There's obviously a fundamental issue that I'm completely overlooking. Any help would be much appreciated
Note that there is a colon after Dlist<double>::CopyAll in the error message so it's not saying that CopyAll cannot access private members. I think what it actually tries to say is that CopyAll is private and you are therefore not allowed to call it from where you're calling it (from outside the class).
If the intention is that you should be able to call CopyAll from outside the class then make it public.
Otherwise, stop calling CopyAll because you're not supposed to call it.