Note that there are some explanatory texts on larger screens.

plurals
  1. POBinary Search Tree LNK2019 and LNK1120 errors
    text
    copied!<p>I've been working on a class project where we have to add an insert, count, remove_max and remove_all function to a binary search tree implementation given to us by the instructor. I've gotten down all of the functions, but for some reason I'm getting these two errors:</p> <pre><code>1&gt;bagexam.obj : error LNK2019: unresolved external symbol "public: __thiscall main_savitch_10::bag&lt;int&gt;::bag&lt;int&gt;(void)" (??0?$bag@H@main_savitch_10@@QAE@XZ) referenced in function "int __cdecl test1(void)" (?test1@@YAHXZ) 1&gt;C:\Users\Filip\Documents\Visual Studio 2012\Projects\Project 3\Debug\Project 3.exe : fatal error LNK1120: 1 unresolved externals </code></pre> <p>And when I double click or "Go to Location" on these errors, it doesn't take me anywhere... so I can't tell where the problem is stemming from. Here is the code that I implemented:</p> <p>insert:</p> <pre><code>template &lt;class Item&gt; void bag&lt;Item&gt;::insert(const Item&amp; entry) // Header file used: bintree.h { binary_tree_node&lt;Item&gt; *cursor; if (root_ptr == NULL) { // Add the first node of the binary search tree: root_ptr = new binary_tree_node&lt;Item&gt;(entry); return; } else { // Move down the tree and add a new leaf: cursor = root_ptr; binary_tree_node&lt;Item&gt; *insert_ptr; insert_ptr = new binary_tree_node&lt;Item&gt; (entry); bool finished = false; while(!finished) { if (cursor-&gt;data() &lt; entry) { if (cursor-&gt;right() == NULL) //Check if this this the largest node of the subtree? { cursor -&gt; right() = insert_ptr; //Add to the right side. finished = true; } cursor = cursor -&gt; right(); } else // entry &lt; (cursor -&gt; data) { if (cursor-&gt;left() == NULL) { cursor-&gt;left() = insert_ptr; //Add to the left side. finished = true; } cursor=cursor-&gt;left(); } } } } </code></pre> <p>count:</p> <pre><code>template &lt;class Item&gt; typename bag&lt;Item&gt;::size_type bag&lt;Item&gt;::count(const Item&amp; target) const { size_type answer = 0; binary_tree_node&lt;Item&gt; *cursor; cursor = root_ptr; if (root_ptr == NULL) return 0; //it's empty so return 0 else { while(cursor!=NULL) { if(cursor-&gt;data() &lt; target) //if target is bigger, go to the right cursor=cursor-&gt;right(); else if(cursor-&gt;data() &gt; target) //target is smaller, go left cursor=cursor-&gt;left(); else if(cursor-&gt;data() == target) //Did we find one? { answer++; //We found one cursor=cursor-&gt;left(); //Go left, there might be another down there } else cout &lt;&lt; "Houston we have a problem!" &lt;&lt; endl; } } return answer; } </code></pre> <p>remove_max:</p> <pre><code>template &lt;class Item&gt; void bst_remove_max(binary_tree_node&lt;Item&gt;*&amp; root_ptr, Item&amp; 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 -&gt; right() == NULL) { removed = root_ptr -&gt; data(); binary_tree_node &lt;Item&gt;* to_delete = root_ptr; root_ptr = root_ptr -&gt; left(); delete to_delete; } else { bst_remove_max(root_ptr -&gt; right(), removed); } } </code></pre> <p>remove_all:</p> <pre><code>template &lt;class Item&gt; typename bag&lt;Item&gt;::size_type bst_remove_all (binary_tree_node&lt;Item&gt;*&amp; root_ptr, const Item&amp; 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&lt;Item&gt; *oldroot_ptr; if (root_ptr == NULL) { // Empty tree return 0; } if (target &lt; root_ptr-&gt;data( )) { // Continue looking in the left subtree bst_remove(root_ptr -&gt; left(), target); } if (target &gt; root_ptr-&gt;data( )) { // Continue looking in the right subtree bst_remove(root_ptr -&gt; right(), target); } if (root_ptr-&gt;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-&gt;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-&gt;left( ), root_ptr-&gt;data( )); return 1 + bst_remove_all(root_ptr, target); } </code></pre> <p>I can't imagine the problem being anywhere else besides the functions that we had to write. Otherwise, there is a header file for the bag, a header for the tree and a test cpp file all provided for us and left unchanged by me.</p> <p>Thanks guys, let me know if you need any more info on the program, and I apologize for posting so much code, I just don't know where to look or what is pertinent to solving this problem as I can't understand the errors :/</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload