Note that there are some explanatory texts on larger screens.

plurals
  1. POFor Looping Link List using Templates
    primarykey
    data
    text
    <p>Having used the various search engines (and the wonderful stackoverflow database), I have found some similar situations, but they are either far more complex, or not nearly as complex as what I'm trying to accomplish.</p> <p><a href="https://stackoverflow.com/questions/5622658/c-list-looping">C++ List Looping</a> <a href="https://stackoverflow.com/questions/550219/link-error-using-templates">Link Error Using Templates</a> <a href="https://stackoverflow.com/questions/12830221/clinked-list-ordering">C++:Linked List Ordering</a> <a href="https://stackoverflow.com/questions/7117844/pointer-address-does-not-change-in-a-link-list">Pointer Address Does Not Change In A Link List</a></p> <p>I'm trying to work with Link List and Node templates to store and print non-standard class objects (in this case, a collection of categorized contacts). Particularly, I want to print multiple objects that have the same category, out of a bunch of objects with different categories. When printing by category, I compare an sub-object tmpCategory (= "business") with the category part of a categorized contact.</p> <p>But how to extract this data for comparison in int main()?</p> <p>Here's what I'm thinking. I create a GetItem member function in LinkList.tem This would initialize the pointer cursor and then run a For loop until the function input matches the iteration number. At which point, GetItem returns object Type using (cursor -> data).</p> <pre><code>template &lt;class Type&gt; Type LinkList&lt;Type&gt;::GetItem(int itemNumber) const { Node&lt;Type&gt;* cursor = NULL; for(cursor = first; cursor != NULL; cursor = (cursor -&gt; next)) { for(int i = 0; i &lt; used; i++) { if(itemNumber == i) { return(cursor -&gt; data); } } } } </code></pre> <p>Here's where int main() comes in. I set my comparison object tmpCategory to a certain value (in this case, "Business"). Then, I run a For loop that iterates for cycles equal to the number of Nodes I have (as determined by a function GetUsed()). Inside that loop, I call GetItem, using the current iteration number. Theoretically, this would let the int main loop return the corresponding Node from LinkList.tem. From there, I call the category from the object inside that Node's data (which currently works), which would be compared with tmpCategory. If there's a match, the loop will print out the entire Node's data object.</p> <pre><code>tmpCategory = "Business"; for(int i = 0; i &lt; myCategorizedContact.GetUsed(); i++) { if(myCategorizedContact.GetItem(i).getCategory() == tmpCategory) cout &lt;&lt; myCategorizedContact.GetItem(i); } </code></pre> <p>The problem is that the currently setup (while it does run), it returns nothing at all. Upon further testing ( cout &lt;&lt; myCategorizedContact.GetItem(i).getCategory() ), I found that it's just printing out the category of the first Node over and over again. I want the overall scheme to evaluate for every Node and print out matching data, not just spit out the same Node.</p> <p>Any ideas/suggestions are greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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