【问题标题】:Templatize LinkedList/Stack, won't work!模板化 LinkedList/Stack,不起作用!
【发布时间】:2011-10-14 19:42:08
【问题描述】:

我正在尝试将链表(/stack/queue)临时化以接受 int 和 double。但到目前为止,我无法让我的代码正常工作。 (我取出列表/队列成员函数只是为了简单地尝试让它工作/在构建时不出错。所以它主要只是堆栈的东西。)没有“模板的东西”代码可以正常工作。如果我错过了任何东西或需要添加任何东西 LMK!

我得到的错误:

1>LinkedList_Stack.obj : 错误 LNK2019: 函数 _main 中引用的无法解析的外部符号“public: __thiscall NumberList::~NumberList(void)”(??1?$NumberList@H@@QAE@XZ)

1>LinkedList_Stack.obj : 错误 LNK2019: 函数 _main 中引用的无法解析的外部符号“public: void __thiscall NumberList::pop(int &)” (?pop@?$NumberList@H@@QAEXAAH@Z)

1>LinkedList_Stack.obj : 错误 LNK2019: 函数 _main 中引用的无法解析的外部符号“public: void __thiscall NumberList::push(int)” (?push@?$NumberList@H@@QAEXH@Z)

1>F:\Documents and Settings\Riotson\Desktop\LinkList\LinkedList_Stack_Queue_BNS11\Debug\LinkedList_Stack_Queue_BNS11.exe : 致命错误 LNK1120: 3 unresolved externals

我在这个网站上阅读了 3 篇不同的帖子,并试图模仿它来进行诱惑(在哪里放置 's 和什么不是),但无法让它发挥作用 =。请帮忙!!!

List.h:

#ifndef LIST_H
#define LIST_H
template<class T>
struct ListNode
{
    T value;    // Value in this node
    struct ListNode<T> *next; // Pointer to the next node
};

    // Class of LinkedList/Stack/Queue
template<class T>
class NumberList
{
private:
    ListNode<T> *head, *rear; // List head pointer

public:
    //Constructor
    NumberList()
    { head = NULL; rear = NULL; }

    //Destructor
    ~NumberList();

    // Linked List Operations
    void displayList() const;

    //Stack operations
    void push(T);
    void pop(T &);
    bool isEmpty();
};

template<class T>
void NumberList<T>::displayList() const
{
    ListNode *nodePtr;

    nodePtr = head;

    while(nodePtr)
    {
        cout << nodePtr->value << endl;

        nodePtr = nodePtr->next;
    }
}
template<class T>
NumberList<T>::~NumberList()
{
    ListNode<T> *nodePtr;
    ListNode<T> *nextNode;

    // Position nodePtr at the head of the list
    nodePtr = head;

    // While nodePtr is not at the end of the list....
    while(nodePtr != NULL)
    {
        // Save a pointer to the next node.
        nextNode = nodePtr->next;

        // Delete the current node.
        delete nodePtr;

        // Position nodePtr at the next node.
        nodePtr = nextNode;
    }
}

template<class T>
void NumberList<T>::push(T num)
{
    ListNode<T> *newNode; // Point to a new node

    // Allocate a new node and store num there.
    newNode = new ListNode;
    newNode->value = num;

    //If there are no nodes in the list
    // make newNode the first node.
    if(isEmpty())
    {
        head = newNode;
        newNode->next = NULL;
    }
    else
    {
        newNode->next = head;
        head = newNode;
    }
}

template<class T>
void NumberList<T>::pop(T &num)
{
    ListNode<T> *temp; // Temporary pointer

    // First make sure stack isn't empty.
    if(isEmpty())//Create a DynIntQueue object.
    {
        cout << "The stack is empty.\n";
    }
    else // pop value off the top of stack
    {
        num = head->value;
        temp = head->next;
        delete head;
        head = temp;
    }
}

// Member function isEmpty returns true if the stack/Queue
//is empty, or false otherwise.
template<class T>
bool NumberList<T>::isEmpty()
{
    bool status;

    if(!head)
        status = true;
    else
        status = false;

    return status;
}
#endif

主要:

#include "List.h"
int main()
{   int catchVar; // To hold value popped off the stack

    // Create a Dynamic IntStack object.
    NumberList<int> stack;

    // Push values onto stack
    cout << "Pushing 1, 2, 3\n";
    stack.push(1);
    stack.push(2);
    stack.push(3);

    cout << endl;

    // Pop the values off the stack
    cout << "Popping...\n";
    stack.pop(catchVar);
    cout << catchVar << endl;
    stack.pop(catchVar);
    cout << catchVar << endl;
    stack.pop(catchVar);
    cout << catchVar << endl;

    // Try to pop another value off the stack.
    cout << "\nAttempting to pop again... " <<endl;
    stack.pop(catchVar);
    cout << endl;

    // Templitization of a Double
    cout << "Pushing a double of 1.5\n";
    stack.push(1.5);

    cout << endl;
    cout << "Popping double 1.5...\n";
    stack.pop(catchVar);
    cout << catchVar << endl;

    cout << "Templitization Succesful\n" << endl;
    cout << endl;

    return 0;
}

【问题讨论】:

    标签: templates stack linked-list


    【解决方案1】:

    不要将模板函数放在 .cpp 中。将它们与头文件一起放入。

    【讨论】:

    • @Redinderien 喜欢吗?加到最后就行了?
    • 是的。完全删除 .cpp 并将其内容移动到标题。模板代码必须在标题中,除非它是专门的,而你的不是。
    • 成功了!!太感谢了!!哈哈,哇,我花了几个小时试图弄清楚,呃。再次感谢!
    • 不客气。您应该阅读stackoverflow.com/questions/495021/…
    猜你喜欢
    • 2014-07-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    相关资源
    最近更新 更多