错误消息
testLL.o:在函数
main
中:testLL.cpp :(。text + 0x13):undefined 引用DblLink<int>::DblLink()
testLL.cpp :(。text + 0x30): 对DblLink<int>::~DblLink()
的未定义引用 testLL.cpp :(。text + 0x4c):未定义引用DblLink<int>::~DblLink()
testLL.o:功能正常menu(DblLink<int>&)': testLL.cpp:(.text+0x14b): undefined reference to
DblLink ::插入件(INT)testLL.cpp:(.text+0x17e): undefined reference to
DblLink ::删除(INT)testLL.cpp:(.text+0x18c): undefined reference to
DblLink :: printIteratively()`
继承我的代码
//File: DblLink.h
// Doubly Linked list class with List Iterator class
#ifndef _DblLink_
#define _DblLink_
#include <assert.h>
#include "Node.h"
#include <iostream>
using namespace std;
// Prototype templace classes if they are to be friends
template <typename eltType> class DblLink;
template <typename eltType> class DblLinkItr;
template <typename eltType> class Node;
template <typename eltType> class DblLink{
public:
//Construct empty LinkedList
DblLink();
//Construct hard copy of another Linkedlist
DblLink(DblLink&);
//Destroy LinkedList
~DblLink();
//Read string of strings into the list object
//void readIntoList(string filename);
//Assign another LinkedList to this LinkedList
DblLink& operator=(const DblLink&);
//Is the LinkedList empty?
bool empty();
bool find(eltType);
//Ordered insert/remove
void insert(eltType);
bool remove(eltType);
//Print the Doubly Linked list iteratively
void printIteratively();
// Print the data recursively with a pointer
//void printPtr();
//Evaluate the Polynomial
//virtual double operator()(double x) const;
private:
//linked list pointer
Node<eltType>* head;
//Get a copy of a (deep) node
Node<eltType>* copy(Node<eltType> *);
//Free nodes of the linked list
void destroy(Node<eltType> *);
//Need this to count nodes in LinkedList
int countNodes(Node<eltType> *) const;
//befriend double linked list iterator
friend class DblLinkItr<eltType>;
friend class Node<eltType>;
};
/******************************************************************************
*******************************DblLink Iterator*******************************
******************************************************************************/
// Create an iterator to provide pointer to the doubly linked list
template <typename eltType> class DblLinkItr{
public:
//Construct a list iterator.
DblLinkItr(DblLink<eltType> &l);
DblLinkItr(const DblLink<eltType> &l);
//set curr to point at the first node of itr
void start();
// Is there more left before null?
bool more();
//Go to curr->next
DblLinkItr operator++(int);
//Get the value out of curr's node
const eltType &operator*() const;
eltType &operator*();
private:
const DblLink<eltType> &itr;
Node<eltType> *curr;
};
#endif
//File: DblLink.cpp
//Ordered Template Doubly Linked List Implementations
#include <assert.h>
#include <iostream>
#include "DblLink.h"
#include "Node.h"
// Construct empty Linked List
template <typename eltType> DblLink<eltType>::DblLink() : head(NULL)
{}
//Copy constructor. copy() does the deep copy
template <typename eltType>
DblLink<eltType>::DblLink(DblLink<eltType> &cl)
{head = copy(cl.head);}
// Free all nodes
template <typename eltType> DblLink<eltType>::~DblLink()
{destroy(head);}
//Assignment operator: copy() does the deep copy
template <typename eltType> DblLink<eltType>
&DblLink<eltType>::operator=(const DblLink<eltType> &cl){
if(this != &cl)
{
destroy(head);
head = copy(cl.head);
}
return *this;
}
//Is the LinkedList empty?
template <typename eltType> inline bool DblLink<eltType>::empty(){
return(head == NULL);}
// Remove all nodes in the list starting at l-
template <typename eltType> void DblLink<eltType>::destroy(Node<eltType> *l)
{
Node<eltType> *doomed = l;
l= l->next;
delete doomed;
}
// Deep copy. copy the source list l one node at a time
template <typename eltType>
Node<eltType>* DblLink<eltType>::copy(Node<eltType> *l)
{
Node<eltType>* first=NULL; //ptr to beginning of copied List
Node<eltType>* last=NULL; //ptr to last item insert in the copy
if (l != NULL)
{
assert((first=last=new Node<eltType>(l->data,NULL,NULL)) != NULL);
for (Node<eltType>* source=l->next; source!=NULL;
source=source->next,last=last->next)
{
last->next = new node<eltType>(source->data, last,NULL);
assert(last->next);
}
}
}
// Ordered Insert
template <typename eltType>
void DblLink<eltType>::insert(eltType x){
//insert at head if new data is less than head or head is empty
if (empty() || x < head->data)
assert(head=new Node<eltType>(x,head,head));
else
{ Node<eltType>* p =head;
// increment p while data is less than x and not equal to end of list
while(p->data< x && p->next != NULL){
p= p->next;
//insert in middle of list
if(p->data => x){
Node<eltType>* new Node<eltType>(x, p->prev, p);
p->prev->next =newNode;
p->prev =newNode;
}
//insert at end of list
if(p->next ==NULL){
Node<eltType>* newNode = new Node<eltType>(x, p, NULL);
p->next = newNode;
}
}
}
}
//Remove a node in an odered list
template <typename eltType>
bool DblLink<eltType>::remove(eltType x){
assert(!empty());
Node<eltType>* p=head;
Node<eltType>* trailp=NULL;
//increment p and trail p while data is not equal to node or at end of list
while (p != NULL &&p->data! = x)
{
trailp= p;
p= p->next;
}
//if object is not found in the list
if(p==NULL)
return(false); // x is not in the list
//if object is found at head of list
else if(p == head){
head->next->prev = NULL;
head = head->next;
delete p;
return(true);
}
//if object is found in the middle of list
else if(p->data == x && p->next !=NULL){
trailp->next = p->next;
p->next->prev = trailp;
delete p;
return(true);
}
//if object is found at end of list
else{
p->prev=NULL;
trailp->next=NULL;
delete p;
return(true);
}
}
template <typename eltType>
void DblLink<eltType>::printIteratively();
{
cout<< "Doubly Linked List printed Iteratively" <<endl;
DblLinkItr<eltType> lt(l);
for(lt.start(); lt.more(); lt++)
cout << (*lt) << " ";
}
/******************************************************************************
*************************DblLink Iterator Implementations*********************
******************************************************************************/
//Create an iterator to provide interface for DblLink
template <typename eltType>
DblLinkItr<eltType>::DblLinkItr(DblLink<eltType> &l): itr(l),curr(l.head)
{}
template <typename eltType>
DblLinkItr<eltType>::DblLinkItr(const DblLink<eltType> &l): itr(l),curr(l.head)
{}
//Set curr to point at itr's head
template <typename eltType>
void DblLinkItr<eltType>::start(void)
{curr= itr.head;}
//is curr at the end of the list?
template <typename eltType>
bool DblLinkItr<eltType>::more(void)
{return curr != NULL;}
//Move curr to next node
template <typename eltType>
DblLinkItr<eltType> DblLinkItr<eltType>::operator++(int)
{
DblLinkItr <eltType> temp = *this;
assert(curr != NULL);
curr = curr->next;
return(temp);
}
//Return data in curr's node.
template <typename eltType> eltType &DblLinkItr<eltType>::operator*()
{
assert(curr !=NULL);
return curr->data;
}
template <typename eltType> const eltType &DblLinkItr<eltType>::operator*() const
{
assert(curr !=NULL);
return curr->data;
}
/* Filename: testLL.cpp */
/* Purpose: This program tests the DblLink class with a */
/* menu-driven interface that permits the user*/
/* to perform a number of operations one or */
/* all of a Doubly Linked list of intergers */
/****************************************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include "DblLink.h"
#include "Node.h"
using namespace std;
/*********************************************************************/
/* Function name: menu */
/* Description: Displays menu, gets input from user, and carries */
/* out requested operation. */
/* Parameters: LinkedList<int>& Numbers - IMPORT/EXPORT - */
/* Object to store and interface with integers */
/* Return Value: None */
/*********************************************************************/
void menu(DblLink<int>& Numbers);
/*********************************************************************/
/* Function name: choiceQuit */
/* Description: Confirms whether user wants to quit the program */
/* Parameters: None */
/* Return Value: bool */
/*********************************************************************/
bool choiceQuit();
int main()
{
DblLink<int> Numbers;
menu(Numbers);
return 0;
}
// Displays menu, gets input from user, and carries out requested operation.
void menu(DblLink<int>& Numbers)
{
int menuChoice;
bool quit=false;
int userInput;
do
{
cout<<"\n";
cout<<"===================";
cout<<"1. Insert";
cout<<"2. Remove";
cout<<"3. Print Foward";
cout<<"4. Print";
cout<<"5. Exit";
cout<<"===================";
cout<<"\n";
cin>>menuChoice;
switch(menuChoice)
{
case 1:
cout<< "Enter a Number to insert into the list\n->";
cin>> userInput;
Numbers.insert(userInput);
break;
case 2:
cout<< "Enter a Number to remove from the list\n->";
cin>> userInput;
Numbers.remove(userInput);
break;
case 3:
Numbers.printIteratively();
break;
case 4:
break;
case 5:
quit=choiceQuit();
break;
default:
cout<<"\nAn invalid choice was made, please try again.\n";
}
} while(quit==false);
}
// Confirms whether user wants to quit the program
bool choiceQuit()
{
string confirm;
cout<<"\nAre you sure you want to quit? (Yes/No)\n";
cin>>confirm;
while(confirm!="No"&&confirm!="no"&&confirm!="Yes"&&confirm!="yes")
{ cout<<"\nAre you sure you want to quit? (Yes/No)\n";
cin>>confirm;
}
return(confirm!="No"&& confirm!="no");
}
// File: Node.h
// Doubly-linked list node definition/implementation
#ifndef NODE_H
#define NODE_H
using namespace std;
// Need to prototype template classes if they are to be friends
template <class eltType> class DblLink;
template <class eltType> class DblLinkItr;
template <class eltType> class Node
{ private:
Node(eltType info, Node *pLink = 0, Node *rLink=0)
: data(info), prev(pLink), next(rLink) {};
eltType data;
Node* prev;
Node* next;
friend class DblLink<eltType>;
friend class DblLinkItr<eltType>;
};
#endif