我一直在研究一个类项目,我们必须将一个insert,count,remove_max和remove_all函数添加到教师给我们的二进制搜索树实现中。我已经完成了所有功能,但出于某种原因,我遇到了这两个错误:
1>bagexam.obj : error LNK2019: unresolved external symbol "public: __thiscall main_savitch_10::bag<int>::bag<int>(void)" (??0?$bag@H@main_savitch_10@@QAE@XZ) referenced in function "int __cdecl test1(void)" (?test1@@YAHXZ)
1>C:\Users\Filip\Documents\Visual Studio 2012\Projects\Project 3\Debug\Project 3.exe : fatal error LNK1120: 1 unresolved externals
当我双击或“转到位置”这些错误时,它不会带我到任何地方......所以我无法分辨出问题出在哪里。这是我实现的代码:
插入:
template <class Item>
void bag<Item>::insert(const Item& entry)
// Header file used: bintree.h
{
binary_tree_node<Item> *cursor;
if (root_ptr == NULL)
{ // Add the first node of the binary search tree:
root_ptr = new binary_tree_node<Item>(entry);
return;
}
else
{ // Move down the tree and add a new leaf:
cursor = root_ptr;
binary_tree_node<Item> *insert_ptr;
insert_ptr = new binary_tree_node<Item> (entry);
bool finished = false;
while(!finished)
{
if (cursor->data() < entry)
{
if (cursor->right() == NULL) //Check if this this the largest node of the subtree?
{
cursor -> right() = insert_ptr; //Add to the right side.
finished = true;
}
cursor = cursor -> right();
}
else // entry < (cursor -> data)
{
if (cursor->left() == NULL)
{
cursor->left() = insert_ptr; //Add to the left side.
finished = true;
}
cursor=cursor->left();
}
}
}
}
数:
template <class Item>
typename bag<Item>::size_type bag<Item>::count(const Item& target) const
{
size_type answer = 0;
binary_tree_node<Item> *cursor;
cursor = root_ptr;
if (root_ptr == NULL)
return 0; //it's empty so return 0
else
{
while(cursor!=NULL)
{
if(cursor->data() < target) //if target is bigger, go to the right
cursor=cursor->right();
else if(cursor->data() > target) //target is smaller, go left
cursor=cursor->left();
else if(cursor->data() == target) //Did we find one?
{
answer++; //We found one
cursor=cursor->left(); //Go left, there might be another down there
}
else cout << "Houston we have a problem!" << endl;
}
}
return answer;
}
remove_max:
template <class Item>
void bst_remove_max(binary_tree_node<Item>*& root_ptr, Item& removed)
// Precondition: root_ptr is a root pointer of a non-empty binary
// search tree.
// Postcondition: The largest item in the binary search tree has been
// removed, and root_ptr now points to the root of the new (smaller)
// binary search tree. The reference parameter, removed, has been set
// to a copy of the removed item.
{
if (root_ptr == NULL)
{
return;
}
else if( root_ptr -> right() == NULL)
{
removed = root_ptr -> data();
binary_tree_node <Item>* to_delete = root_ptr;
root_ptr = root_ptr -> left();
delete to_delete;
}
else
{
bst_remove_max(root_ptr -> right(), removed);
}
}
REMOVE_ALL:
template <class Item>
typename bag<Item>::size_type bst_remove_all (binary_tree_node<Item>*& root_ptr, const Item& target)
// Precondition: root_ptr is a root pointer of a binary search tree
// or may be NULL for an empty tree).
// Postcondition: All copies of target have been removed from the tree
// has been removed, and root_ptr now points to the root of the new
// (smaller) binary search tree. The return value tells how many copies
// of the target were removed.
{
binary_tree_node<Item> *oldroot_ptr;
if (root_ptr == NULL)
{ // Empty tree
return 0;
}
if (target < root_ptr->data( ))
{ // Continue looking in the left subtree
bst_remove(root_ptr -> left(), target);
}
if (target > root_ptr->data( ))
{ // Continue looking in the right subtree
bst_remove(root_ptr -> right(), target);
}
if (root_ptr->left( ) == NULL)
{ // Target was found and there is no left subtree, so we can
// remove this node, making the right child be the new root.
oldroot_ptr = root_ptr;
root_ptr = root_ptr->right( );
delete oldroot_ptr;
return 1;
}
// If code reaches this point, then we must remove the target from
// the current node. We'll actually replace this target with the
// maximum item in our left subtree. We then continue
// searching for more copies of the target to remove.
// This continued search must start at the current root (since
// the maximum element that we moved up from our left subtree
// might also be a copy of the target).
bst_remove_max(root_ptr->left( ), root_ptr->data( ));
return 1 + bst_remove_all(root_ptr, target);
}
我无法想象除了我们必须编写的函数之外的其他任何问题。否则,包有一个头文件,树的标题和测试cpp文件都为我们提供,并保持不变。
谢谢大家,如果您需要有关该计划的更多信息,请告诉我,我为发布这么多代码而道歉,我只是不知道在哪里寻找或解决这个问题的相关因素我不能理解错误:/