开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜