LNK2019:未解析的外部符号C ++

时间:2015-04-09 12:23:58

标签: c++ class templates lnk2019

我搜索并搜索了几个小时,似乎无法找到解决方法。

我正在构建一个链接列表,并将其作为一个int类型进行测试,一切都运行良好,现在我将它转换为类模板之后就会出现这个。

希望其他一些人能看到我所缺少的东西。我也清理过并重建了。 Ps我也尝试将链接器系统从控制台切换到Windows,它实际上买了更多的错误。

以下是错误示例:

  

错误1错误LNK2019:函数_main D:\ ... \ Source中引用了未解析的外部符号“public:__thiscall LinkedList :: LinkedList(void)”(?? 0?$ LinkedList @ H @@ QAE @ XZ) .obj Text-LinkedList

LinkedList.h

            #pragma once

            //***********************************************************
            // Author: Blake McLeod
            //
            // This class specifies the members to implement the basic
            // properties of a linked list.
            //***********************************************************
            #include <iostream>
            #include "Node.h"
            using namespace std;

            template <class T>
            class LinkedList
            {
            public:
                            //default constructor
                            //Initializes the list to an empty state.
                            //Postcondition: first = NULL, last = NULL, count = 0;
                            LinkedList();

                            //copy constructor
                            LinkedList(const LinkedList<T> &otherList);

                            //destructor
                            //Deletes all the nodes from the list.
                            //Postcondition: The list object is destroyed.
                            ~LinkedList();

                            //Function to delete all the nodes from the list.
                            //Postcondition: first = NULL, last = NULL, count = 0;
                            void destroy();

                            //Function to determine whether the list is empty.
                            //Postcondition: Returns true if the list is empty, otherwise
                            // it returns false.
                            bool isEmpty() const;

                            //Function to output the data contained in each node.
                            //Postcondition: none
                            void print() const;

                            //Function to return the number of nodes in the list.
                            //Postcondition: The value of count is returned.
                            int getLength() const;

                            //Function to return the first element of the list.
                            //Precondition: The list must exist and must not be empty.
                            //Postcondition: If the list is empty, the program terminates;
                            // otherwise, the first element of the list is returned.
                            T firstNode() const;

                            //Function to return the last element of the list.
                            //Precondition: The list must exist and must not be empty.
                            //Postcondition: If the list is empty, the program
                            //  terminates; otherwise, the last
                            // element of the list is returned.
                            T lastNode() const;

                            //Function to determine whether searchItem is in the list.
                            //Postcondition: Returns true if searchItem is in the list,
                            // otherwise the value false is returned.
                            bool search(const T &searchItem) const;

                            //Function to insert newItem in the list.
                            //Postcondition: first points to the new list, newItem
                            // is inserted at the proper place in the list, and
                            // count is incremented by 1.
                            void insert(const T &newItem);

                            //Function to insert newItem at the beginning of the list.
                            //Postcondition: first points to the new list, newItem is
                            // inserted at the beginning of the list, last points to
                            // the last node in the list, and count is incremented by
                            // 1.
                            void insertFirst(const T &newItem);

                            //Function to insert newItem at the end of the list.
                            //Postcondition: first points to the new list, newItem is
                            // inserted at the end of the list, last points to the
                            // last node in the list, and count is incremented by 1.
                            void insertLast(const T &newItem);

                            //Function to delete deleteItem from the list.
                            //Postcondition: If found, the node containing deleteItem is
                            // deleted from the list. first points to the first node,
                            // last points to the last node of the updated list, and
                            // count is decremented by 1.
                            void deleteNode(const T &deleteItem);

                            //Function to make a copy of otherList.
                            //Postcondition: A copy of otherList is created and assigned
                            // to this list.
                            void copyList(const LinkedList<T> &otherList);

                            //Overload the assignment operator.
                            const LinkedList<T>& operator= (const LinkedList<T> &otherList);

            private:
                            int length;                                 //Length of linked list
                            Node<T> *head;                   //Pointer to first node
                            Node<T> *trailer;                       //Pointer to last node
                            Node<T> *current;           //Pointer to current node
            };

LinkedList.cpp

            //***********************************************************
            // Author: Blake McLeod
            //
            // This file defines all function members of the LinkedList.h
            // class file, functions are designed to handle various basic
            // tasks required to manage a linked list.
            //***********************************************************
            #include "LinkedList.h"
            #include <iostream>
            #include <vector>
            #include <string>
            using namespace std;

            //default constructor
            //Initializes the list to an empty state.
            //Postcondition: first = NULL, last = NULL, count = 0;
            template <class T>
            LinkedList<T>::LinkedList()
            {
                            head = NULL;
                            trailer = NULL;
                            length = 0;

                            cout << "Created an empty linked list object." << endl;
            }

            //copy constructor
            template <class T>
            LinkedList<T>::LinkedList(const LinkedList<T> &otherList)
            {
                            head = NULL;
                            copyList(otherList);
            }

            //destructor
            //Deletes all the nodes from the list.
            //Postcondition: The list object is destroyed.
            template <class T>
            LinkedList<T>::~LinkedList()
            {
                            //Clear all elements in linked list
                            destroy();
            }

            //Function to delete all the nodes from the list.
            //Postcondition: first = NULL, last = NULL, count = 0;
            template <class T>
            void LinkedList<T>::destroy()
            {
                            Node<T> *temp;

                            while (head)
                            {
                                            temp = head;
                                            head = head->link;
                                            delete temp;
                            }

                            trailer = NULL;
                            length = 0;
            }

            //Function to determine whether the list is empty.
            //Postcondition: Returns true if the list is empty, otherwise
            // it returns false.
            template <class T>
            bool LinkedList<T>::isEmpty() const
            {
                            return (head == NULL);
            }

            //Function to output the data contained in each node.
            //Postcondition: none
            template <class T>
            void LinkedList<T>::print() const
            {
                            Node<T> *current; //pointer to traverse the list
                            current = head; //set current point to the first node

                            while (current != NULL) //while more data to print
                            {
                                            cout << current->data << " ";
                                            current = current->link;
                            }
            }

            //Function to return the number of nodes in the list.
            //Postcondition: The value of count is returned.
            template <class T>
            int LinkedList<T>::getLength() const
            {
                            return length;
            }

            //Function to return the first element of the list.
            //Precondition: The list must exist and must not be empty.
            //Postcondition: If the list is empty, the program terminates;
            // otherwise, the first element of the list is returned.
            template <class T>
            T LinkedList<T>::firstNode() const
            {
                            if (head != NULL)
                                            return head->data;
                            else
                                            return 0;
            }

            //Function to return the last element of the list.
            //Precondition: The list must exist and must not be empty.
            //Postcondition: If the list is empty, the program
            //  terminates; otherwise, the last
            // element of the list is returned.
            template <class T>
            T LinkedList<T>::lastNode() const
            {
                            if (trailer != NULL)
                                            return trailer->data;
                            else
                                            return 0;
            }

            //Function to determine whether searchItem is in the list.
            //Postcondition: Returns true if searchItem is in the list,
            // otherwise the value false is returned.
            template <class T>
            bool LinkedList<T>::search(const T &searchItem) const
            {
                            bool found = false;
                            Node<T> *current;

                            current = head;

                            while (current != NULL && !found)
                            {
                                            if (current->data >= searchItem)
                                                            found = true;
                                            else
                                                            current = current->link;
                            }

                            if (found)
                                            found = (current->data == searchItem);

                            return found;
            }

            //Function to insert newItem in the list.
            //Postcondition: first points to the new list, newItem
            // is inserted at the proper place in the list, and
            // count is incremented by 1.
            template <class T>
            void LinkedList<T>::insert(const T &newItem)
            {
                            Node<T> *current = NULL;
                            Node<T> *trailCurrent = NULL;
                            Node<T> *newNode = NULL;

                            bool found;

                            newNode = new Node<T>;
                            newNode->data = newItem;
                            newNode->link = NULL;

                            if (head == NULL)
                            {
                                            head = newNode;
                                            trailer = newNode;
                                            length++;
                            }
                            else
                            {
                                            current = head;
                                            found = false;

                                            while (current != NULL && !found)
                                            {
                                                            if (current->data >= newItem)
                                                                            found = true;
                                                            else
                                                            {
                                                                            trailCurrent = current;
                                                                            current = current->link;
                                                            }
                                            }

                                            if (current == head)
                                            {
                                                            newNode->link = head;
                                                            head = newNode;
                                                            length++;
                                            }
                                            else
                                            {
                                                            trailCurrent->link = newNode;
                                                            newNode->link = current;

                                                            if (current == NULL)
                                                                            trailer = newNode;

                                                            length++;
                                            }
                            }

            }

            //Function to delete deleteItem from the list.
            //Postcondition: If found, the node containing deleteItem is
            // deleted from the list. first points to the first node,
            // last points to the last node of the updated list, and
            // count is decremented by 1.
            template <class T>
            void LinkedList<T>::deleteNode(const T &deleteItem)
            {
                            Node<T> *current = NULL;
                            Node<T> *trailCurrent = NULL;
                            bool found;

                            if (head == NULL) //if list is empty
                                            cout << "Error: Cannot delete node from an empty list." << endl;
                            else
                            {
                                            current = head;
                                            found = false;

                                            while (current != NULL && !found) //search list
                                            {
                                                            if (current->data >= deleteItem)
                                                                            found = true;
                                                            else
                                                            {
                                                                            trailCurrent = current;
                                                                            current = current->link;
                                                            }
                                            }

                                            if (current == NULL)
                                                            cout << "The item to be deleted is not in the list." << endl;
                                            else if (current->data == deleteItem)
                                            {
                                                            if (head == current)
                                                            {
                                                                            head = head->link;
                                                                            if (head == NULL)
                                                                                            trailer = NULL;

                                                                            delete current;
                                                            }
                                                            else
                                                            {
                                                                            trailCurrent->link = current->link;

                                                                            if (current == trailer)
                                                                                            trailer = trailCurrent;

                                                                            delete current;
                                                            }
                                                            length--;
                                            }
                                            else
                                                            cout << "Error: Item was not found in the list." << endl;
                            }
            }

            //Function to make a copy of otherList.
            //Postcondition: A copy of otherList is created and assigned
            // to this list.
            template <class T>
            void LinkedList<T>::copyList(const LinkedList<T> &otherList)
            {
                            Node<T> *newNode, *currentNode;

                            //ensure this list is empty
                            if (head != NULL)
                                            destroy();

                            //if otherList is empty, create empty list
                            if (otherList.head == NULL)
                            {
                                            head = NULL;
                                            trailer = NULL;
                                            length = 0;
                            }
                            else
                            {
                                            currentNode = otherList.head;
                                            length = otherList.length;

                                            head = new Node<T>; //create first node
                                            head->data = currentNode->data; //copy data
                                            head->link = NULL; //set the link field of the node to NULL
                                            trailer = head; //make last point to the head node
                                            currentNode = currentNode->link; //make currentNode point to the next node

                                            while (currentNode != NULL)
                                            {
                                                            newNode = new Node<T>; //create a node
                                                            newNode->data = currentNode->data; //copy the info
                                                            newNode->link = NULL; //set the link of newNode to NULL
                                                            trailer->link = newNode; //attach newNode after last
                                                            trailer = newNode; //make last point to the actual last node
                                                            currentNode = currentNode->link; //make currentNode point to the next node
                                            }
                            }
            }

            //Overload the assignment operator.
            template <class T>
            const LinkedList<T>& LinkedList<T>::operator= (const LinkedList<T> &otherList)
            {
                            if (this != &otherList)
                            {
                                            copyList(otherList);
                            }
                            return *this;
            }

Source.cpp

            //**********************************************************
            // Author: D.S. Malik
            //
            // This program tests the various operations on an ordered
            // linked list.
            //**********************************************************
            #include <iostream>
            #include "LinkedList.h"
            using namespace std;

            int main()
            {
                            LinkedList<int> list1, list2;
                            int num;
                            cout << "Line 7: Enter numbers ending with -999." << endl;
                            cin >> num;

                            while (num != -999)
                            {
                                            list1.insert(num);
                                            cin >> num;
                            }
                            cout << endl;

                            cout << "Line 15: list1: ";
                            list1.print();
                            cout << endl;

                            list2 = list1;
                            cout << "Line 19: list2: ";
                            list2.print();
                            cout << endl;

                            cout << "Line 22: Enter the number to be deleted: ";
                            cin >> num;
                            cout << endl;

                            list2.deleteNode(num);
                            cout << "Line 26: After deleting " << num << ", list2: " << endl;

                            list2.print();
                            cout << endl;

                            system("pause");
                            return 0;
            }

对于大量代码感到抱歉,但希望有更多信息可以提供帮助。

0 个答案:

没有答案