当我尝试编译main的代码时,我的编译器一直说mergeLists()是未定义的。但是,我期望在之前定义它(在orderLinkedList类中。另外,我知道我将它定义为成员而我没有放置一个调用对象,但是当我将它重新定义为非成员时它会抛出它 / -------------------------------------------- 在main.cpp中包含的文件中:4:0: orderedLinkedList.h:204:6:注意:候选:模板void mergeLists(orderedLinkedList&,orderedLinkedList&) void mergeLists(orderedLinkedList& list1,orderedLinkedList& list2){ ^ orderedLinkedList.h:204:6:注意:模板参数扣除/替换失败: main.cpp:43:31:注意:'unorderedLinkedList'不是从'orderedLinkedList'派生的 mergeLists(list,otherList); / ------------------------------------------------ 除了其他错误。我错过了什么?
template <class Type>
void orderedLinkedList<Type>::mergeLists(orderedLinkedList<Type> & list1, orderedLinkedList<Type> & list2) {
if(list2.first != NULL) {
nodeType <Type> * current = list2.first;
nodeType <Type> * trail = NULL;
for(int cntr = 0; cntr <= list2.count; cntr++) {
list1.insert(current->info);
trail = current;
current = current->link;
delete trail;
}
list2.count = 0;
list2.first = list2.last = NULL;
}
}
#ifndef H_orderedListType
#define H_orderedListType
#include "linkedList.h"
using namespace std;
template <class Type>
class orderedLinkedList: public linkedListType<Type>
{
typedef linkedListType<Type> super;
using super::first;
using super::count;
using super::last;
public:
bool search(const Type& searchItem) 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.
void insert(const Type& newItem);
//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 insertFirst(const Type& newItem);
//Function to insert newItem in the list.
//Because the resulting list must be sorted, newItem is
//inserted at the proper in the list.
//This function uses the function insert to insert newItem.
//Postcondition: first points to the new list, newItem is
// inserted at the proper in the list,
// and count is incremented by 1.
void insertLast(const Type& newItem);
//Function to insert newItem in the list.
//Because the resulting list must be sorted, newItem is
//inserted at the proper in the list.
//This function uses the function insert to insert newItem.
//Postcondition: first points to the new list, newItem is
// inserted at the proper in the list,
// and count is incremented by 1.
void deleteNode(const Type& deleteItem);
//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 of the
// new list, and count is decremented by 1.
// If deleteItem is not in the list, an
// appropriate message is printed.
//void mergeLists(orderedLinkedList<Type> & list1, orderedLinkedList<Type> & list2);
};
/*******************************************************************************
********************************************************************************
*******************************************************************************/
template <class Type>
bool orderedLinkedList<Type>::search(const Type& searchItem) const
{
bool found = false;
nodeType<Type> *current; //pointer to traverse the list
current = first; //start the search at the first node
while (current != NULL && !found)
if (current->info >= searchItem)
found = true;
else
current = current->link;
if (found)
found = (current->info == searchItem); //test for equality
return found;
}//end search
template <class Type>
void orderedLinkedList<Type>::insert(const Type& newItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
nodeType<Type> *newNode; //pointer to create a node
bool found;
newNode = new nodeType<Type>; //create the node
newNode->info = newItem; //store newItem in the node
newNode->link = NULL; //set the link field of the node
//to nullptr
if (first == NULL) //Case 1
{
first = newNode;
last = newNode;
count++;
}
else
{
current = first;
found = false;
while (current != NULL && !found) //search the list
if (current->info >= newItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if (current == first) //Case 2
{
newNode->link = first;
first = newNode;
count++;
}
else //Case 3
{
trailCurrent->link = newNode;
newNode->link = current;
if (current == NULL)
last = newNode;
count++;
}
}//end else
}//end insert
template<class Type>
void orderedLinkedList<Type>::insertFirst(const Type& newItem)
{
insert(newItem);
}//end insertFirst
template<class Type>
void orderedLinkedList<Type>::insertLast(const Type& newItem)
{
insert(newItem);
}//end insertLast
template<class Type>
void orderedLinkedList<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if (first == NULL) //Case 1
cout << "Cannot delete from an empty list." << endl;
else
{
current = first;
found = false;
while (current != NULL && !found) //search the list
if (current->info >= deleteItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if (current == NULL) //Case 4
cout << "The item to be deleted is not in the "
<< "list." << endl;
else
if (current->info == deleteItem) //the item to be
//deleted is in the list
{
if (first == current) //Case 2
{
first = first->link;
if (first == NULL)
last = NULL;
delete current;
}
else //Case 3
{
trailCurrent->link = current->link;
if (current == last)
last = trailCurrent;
delete current;
}
count--;
}
else //Case 4
cout << "The item to be deleted is not in the "
<< "list." << endl;
}
}//end deleteNode
template <class Type>
void mergeLists(orderedLinkedList<Type> & list1, orderedLinkedList<Type> & list2) {
if(list2.first != NULL) {
nodeType <Type> * current = list2.first;
nodeType <Type> * trail = NULL;
for(int cntr = 0; cntr <= list2.count; cntr++) {
list1.insert(current->info);
trail = current;
current = current->link;
delete trail;
}
list2.count = 0;
list2.first = list2.last = NULL;
}
}
#endif
我的调用就是mergeLists(list,otherlist);列表和其他列表是int
类型的列表