开发者

undeclared first use this function - compile error C++ LinkedList

开发者 https://www.devze.com 2023-04-02 05:01 出处:网络
I have a function that inserts a new node at the tail end of a linkedlist: void LinkedList::insert(Node* previousPtr, Node::value_type& newData)

I have a function that inserts a new node at the tail end of a linkedlist:

void LinkedList::insert(Node* previousPtr, Node::value_type& newData)
{
    Node *insertPtr;

    insertPtr->setData(newData);

    insertPtr->setNext(previousPtr->getNextPtr());

    previousPtr->setNext(insertPtr);
}
开发者_C百科

In another function I am trying to call the previous:

void copyData(Node* sourcePtr, Node*& headPtr, Node*& tailPtr)
{
    ...//other code
    insert(tailPtr, sourcePtr->getData());
    ...//other code
}

The compiler gives an error of: "insert" undeclared first use this function. What am I missing?


You are missing something like

some_linked_list->insert(some_node_ptr, ...)

or you could make copydata a member of the LinkedList class:

void LinkedList::copyData(Node* sourcePtr, Node*& headPtr, Node*& tailPtr) 


LinkedList::insert is a method in your LinkedList class. You would need an instance of that class to call it.

LinkedList *myLinkedList = new LinkedList();
myLinkedList->insert( ... );
0

精彩评论

暂无评论...
验证码 换一张
取 消