0
我從我的代碼中收到以下錯誤。不知道如何解決它。無法找到問題。錯誤LNK2019:無法解析的外部符號「公共:結構節點<int> * __thiscall表<int> :: find_ptr(int)」
error LNK2019: unresolved external symbol "public: struct Node<int> * __thiscall Table<int>::find_ptr(int)" ([email protected][email protected]@@[email protected]@@[email protected]) referenced in function "public: void __thiscall Table<int>::insert(int const &)" ([email protected][email protected]@@[email protected])
以下是我的代碼:
以下是我link2.h文件:
#ifndef LINK2_H
#define LINK2_H
#include <stdlib.h> // Provides size_t
template <class Item>
struct Node
{
Item data;
Node *link;
};
// FUNCTIONS for the linked list toolkit
template <class Item>
size_t list_length(Node<Item>* head_ptr);
template <class Item>
void list_head_insert(Node<Item>*& head_ptr, const Item& entry);
template <class Item>
void list_insert(Node<Item>*& previous_ptr, const Item& entry);
template <class Item>
Node<Item>* list_search(Node<Item>* head_ptr, const Item& target);
template <class Item, class SizeType>
Node<Item>* list_locate(Node<Item>* head_ptr, SizeType position);
template <class Item>
void list_head_remove(Node<Item>*& head_ptr);
template <class Item>
void list_remove(Node<Item>* previous_ptr);
template <class Item>
void list_clear(Node<Item>*& head_ptr);
template <class Item>
void list_copy
(Node<Item>* source_ptr, Node<Item>*& head_ptr, Node<Item>*& tail_ptr);
template <class Item>
void list_piece
(Node<Item>* source_ptr, Node<Item>* end_ptr,
Node<Item>*& head_ptr, Node<Item>*& tail_ptr);
#include "link2.template" // Include the implementation
#endif
以下是我link2.template文件:
#include <assert.h> // Provides assert
#include <stdlib.h> // Provides NULL and size_t
template <class Item>
size_t list_length(Node<Item>* head_ptr)
// Library facilities used: stdlib.h
{
Node<Item> *cursor;
size_t answer;
answer = 0;
for(cursor = head_ptr; cursor != NULL; cursor = cursor->link)
answer++;
return answer;
}
template <class Item>
void list_head_insert(Node<Item>*& head_ptr, const Item& new_item)
{
Node<Item> *insert_ptr;
insert_ptr = new Node<Item>;
insert_ptr->data = new_item;
insert_ptr->link = head_ptr;
head_ptr = insert_ptr;
}
template <class Item>
void list_insert(Node<Item>* &previous_ptr, const Item& new_item)
{
Node<Item> *insert_ptr;
insert_ptr = new Node<Item>;
insert_ptr->data = new_item;
insert_ptr->link = previous_ptr->link;
previous_ptr->link = insert_ptr;
}
template <class Item>
Node<Item>* list_search(Node<Item>* head_ptr, const Item& target)
{
Node<Item> *cursor;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link)
if (cursor->data == target)
return cursor;
return NULL;
}
template <class Item, class SizeType>
Node<Item>* list_locate(Node<Item>* head_ptr, SizeType position)
// Library facilities assert.h, stdlib.h
{
Node<Item> *cursor;
size_t i;
assert(position > 0);
cursor = head_ptr;
for(i = 1; (i != position) && (cursor != NULL); i++)
cursor = cursor->link;
return cursor;
}
template <class Item>
void list_head_remove(Node<Item>*& head_ptr)
{
Node<Item> *remove_ptr;
remove_ptr = head_ptr;
head_ptr = head_ptr->link;
delete remove_ptr;
}
template <class Item>
void list_remove(Node<Item>* previous_ptr)
{
Node<Item> *remove_ptr;
remove_ptr = previous_ptr->link;
previous_ptr->link = remove_ptr->link;
delete remove_ptr;
}
template <class Item>
void list_clear(Node<Item>*& head_ptr)
// Library facilities used: stdlib.h
{
while (head_ptr != NULL)
list_head_remove(head_ptr);
}
template <class Item>
void list_copy
(Node<Item>* source_ptr, Node<Item>*& head_ptr, Node<Item>*& tail_ptr)
// Library facilities used: stdlib.h
{
head_ptr = NULL;
tail_ptr = NULL;
// Handle the case of the empty list
if (source_ptr == NULL)
return;
// Make the head node for the newly created list, and put data in it
list_head_insert(head_ptr, source_ptr->data);
tail_ptr = head_ptr;
// Copy the rest of the nodes one at a time, adding at the tail of new list
for (source_ptr = source_ptr->link; source_ptr != NULL; source_ptr = source_ptr->link)
{
list_insert(tail_ptr, source_ptr->data);
tail_ptr = tail_ptr->link;
}
}
template <class Item>
void list_piece
(Node<Item>* start_ptr, Node<Item>* end_ptr, Node<Item>*& head_ptr, Node<Item>*& tail_ptr)
// Library facilities used: assert.h, stdlib.h
{
head_ptr = NULL;
tail_ptr = NULL;
// Handle the case of the empty list
if (start_ptr == NULL)
return;
// Make the head node for the newly created list, and put data in it
list_head_insert(head_ptr, start_ptr->data);
tail_ptr = head_ptr;
if (start_ptr == end_ptr)
return;
// Copy the rest of the nodes one at a time, adding at the tail of new list
for (start_ptr = start_ptr->link; start_ptr != NULL; start_ptr = start_ptr
->link)
{
list_insert(tail_ptr, start_ptr->data);
tail_ptr = tail_ptr->link;
if (start_ptr == end_ptr)
return;
}
}
以下是我表2 .h文件:
#ifndef TABLE_H
#define TABLE_H
#include<cstdlib>
#include"link2.h" // provide list toolkit
template<class Item>
class Table
{
public:
// TYPEDEF
typedef std::size_t size_type; //synonym for size_t
static const size_type TABLESIZE = 10;
Table();
Table(const Table& source);
~Table();
void operator =(const Table& source);
// CONST Member functions
size_type size() const{ return totalRecords; }
bool is_present(const Item& target) const;
// void find(int key, bool& found, RecordType& result) const;
// MODIFICATION Member functions
void insert(const Item& entry);
void remove(int key);
void printTable();
Node<Item> *find_ptr(int key);
private:
size_type totalRecords;
Node<Item> *dataArray[TABLESIZE];
//HELPER Functions
std::size_t hash(int key) const;
};
#include "table2.template"
#endif
以下是表2模板文件:
#include "table2.h"
#include "link2.h"
#include<iostream>
template<class Item>
const typename Table<Item>::size_type Table<Item>::TABLESIZE;
template<class Item> // template class
Table<Item>::Table()
{
for(size_type i = 0; i < TABLESIZE; i++)
dataArray[i] = new Node<Item>; //allocate new dynamic memory
totalRecords = 0;
}/* end constructor */
template<class Item>
Table<Item>::Table(const Table& source) //copy constructor
{
Node<Item>* tail_ptr; // dummy node required for list_copy function
for(size_type i = 0; i < TABLESIZE; i++)
dataArray[i] = new Node<Item>; //allocate new dynamic memory if needed
for(size_type i = 0; i < TABLESIZE; i++)
list_copy(source.dataArray[i], dataArray[i], tail_ptr);
totalRecords= source.totalRecords;
}// end copy constructor
template<class Item>
Table<Item>::~Table() //removed <RecordType>
{
for(size_type i = 0; i < TABLESIZE ; i++)
list_clear(dataArray[i]);
totalRecords= 0;
}//end destructor
template<class Item>
void Table<Item>::operator =(const Table& source) //removed <RecordType>
{
Node<Item>* tail_ptr;
if(this == &source) //check for self-assignment
return;
totalRecords = 0; //reset record before copy
for(size_type i = 0; i < TABLESIZE; i++)
{
list_clear(dataArray[i]); //reset list before Copy
}
for(size_type i = 0; i < TABLESIZE; i++)
dataArray[i] = new Node<Item>; //allocate new dynamic memory if needed
for(size_type i = 0; i < TABLESIZE; i++)
{
list_copy(source.dataArray[i], dataArray[i], tail_ptr);
}
totalRecords = source.totalRecords;
}
template<class Item>
void Table<Item>::printTable() //removed <RecordType> after Table
{
Node<Item> *cursor ;
for(size_type i = 0; i < TABLESIZE; i++)
{
cursor = dataArray[i];
std::cout << "[("<<i<<")]----> ";
if(dataArray[i]== NULL)
{
std::cout << " NULL" << std::endl;
continue;
}
while(cursor->link != NULL)
{
std::cout << "[" << cursor-> data << "]-->";
cursor = cursor->link;
}
if(cursor->link == NULL)
std::cout << " NULL" << std::endl;
}
} // end printTable
template<class Item>
void Table<Item>::insert(const Item& entry)
{
if(is_present(entry) == false)
{
Node<Item>* cursor;
cursor = find_ptr(entry);
if (cursor == NULL)
{
list_head_insert(dataArray[hash(entry)],entry);
++totalRecords;
}
else
cursor->data = entry;
}
}
template<class Item>
bool Table<Item>::is_present(const Item& target) const
{
size_type i = hash(target);
if(list_search(dataArray[i], target) == NULL)
return false;
else
return true;
}
template<class Item>
size_t Table<Item>::hash(int key) const
{
return (key % TABLESIZE);
}
template<class Item>
Node<Item> Table<Item>::*find_ptr(int key)
{
Node<Item>* cursor ;
Node<Item>* found ;
for(size_type i = 0; i < TABLESIZE; i++)
{
cursor = dataArray[i];
if(cursor->link == NULL)
continue;
found = list_search(cursor,key);
return found;
}
return NULL;
}
template<class Item>
void Table<Item>::remove(int key)
{
Node<Item>* cursor; // dummy cursor node
Node<Item>* target; // dummy node to be assigned for deletion
bool t = false; // bool value if key element is found
for(size_type i = 0 ; i < TABLESIZE && totalRecords >= 0; i++)
{
cursor = dataArray[i]; // assign cursor to beginning of each list
target = list_search(cursor, key);
if (target != NULL) // if target is not NULL, key found
{
if (target == dataArray[i]) // if the target is the head of list
{
list_head_remove(dataArray[i]); //remove head of list node
totalRecords--; //1 less record
if (totalRecords > TABLESIZE)
totalRecords= 0;
}
else
{
target->data = (dataArray[i]->data);
list_head_remove(dataArray[i]);
totalRecords--; //1 less record
if (totalRecords > TABLESIZE)
totalRecords= 0;
}
}
}
}
以下是我對程序的驅動程序文件:
#include<iostream>
#include<ctime> //allows access to time() function
#include"table2.h"
#include "link2.h"
using namespace std;
int main()
{
Table<int> first; //removed <int> template format parameter
Table<int> second;
srand(time(NULL)); // seed rand() function to time of program execution.
//output following messages
cout << "Instantiate two Table objects\n" << endl;
cout << "\t Total records in first Table object " << first.size() << endl;
cout << "\t Contents of first object display at below:" << endl;
first.printTable(); //call object's printTable function to print
cout << "--------------------------------------------------" << endl;
cout << "\t Total records in second Table object " << second.size() << endl;
cout << "\t Contents of second object display at below:" << endl;
second.printTable(); //call object's printTable function to print
cout << "=====================================================" << endl;
cout << "\n\n**random number generator used." << endl;
for (size_t i = 0; i < 70; i++) // create 70 random numbers for each table
{
first.insert((rand() % 201)); //random number between 0 ~ 201
second.insert((rand() % 201)); //also random between 0 ~201
}
cout << "\t Total records in first Table object " << first.size() << endl;
cout << "\t Contents of first object display at below:" << endl;
first.printTable();
cout << "--------------------------------------------------" << endl;
cout << "\t Total records in second Table object " << second.size() << endl;
cout << "\t Contents of second object display at below:" << endl;
second.printTable();
cout << "===========================================" << endl;
cout << "\n\n\n**remove function: remove 1st obj contents .**\n\n" << endl;
for (int k = 0; k <= 200; k++)
{
first.remove(k);
}
cout << "\t Total records in first Table object " << first.size() << endl;
cout << "\t Contents of first object display at below:" << endl;
first.printTable();
cout << "----------------------------------------" << endl;
cout << "\t Total records in second Table object " << second.size() << endl;
cout << "\t Contents of second object display at below:" << endl;
second.printTable();
cout << "=====================================" << endl;
cout << "\n** Using TBObject1 = TBObject2 . **\n" << endl;
first = second;
cout << "\t Total records in first Table object " << first.size() << endl;
cout << "\t Contents of first object display at below:" << endl;
first.printTable();
cout << "---------------------------------------------" << endl;
cout << "\t Total records in second Table object " << second.size() << endl;
cout << "\t Contents of second object display at below:" << endl;
second.printTable();
system("pause");
return 0;
}