Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Although I'm a bit confused, because you're actual main code is missing. I'm going to assume you have a node, from traversing the link, and now want to print it:</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; using namespace std; // not recommended, but useful // in snippets // T is usually used, but this is of course up to you template &lt;class T&gt; struct Node { typedef T value_type; // a usual typedef value_type info; Node&lt;value_type&gt; *next; }; struct Car { string maker; string year; string model; }; // you had a missing ;, probably copy-paste error // this creates a node. normally you'd want this to be // wrapped into a list class (more on this later) template &lt;typename T&gt; Node&lt;T&gt; *createNode(const T&amp; info = T()) { // allocate node Node&lt;T&gt; *result = new Node&lt;T&gt;; result-&gt;info = info; result-&gt;next = 0; // no next return result; // returning a pointer means // whoever gets this is // responsible for deleting it! } // this is the output function for a node template &lt;typename T&gt; std::ostream&amp; operator&lt;&lt;(std::ostream&amp; sink, const Node&lt;T&gt;&amp; node) { // note that we cannot assume what node contains! // rather, stream the info attached to the node // to the ostream: sink &lt;&lt; node.info; return sink; } // this is the output function for a car std::ostream&amp; operator&lt;&lt;(std::ostream&amp; sink, const Car&amp; car) { // print out car info sink &lt;&lt; "Make: " &lt;&lt; car.maker &lt;&lt; "\nYear: " &lt;&lt; car.year &lt;&lt; "\nModel: " &lt;&lt; car.model &lt;&lt; std::endl; return sink; } int main(void) { // a car list typedef Node&lt;Car&gt; CarList; // a couple cars Car car1 = {"Dodge", "2001", "Stratus"}; Car car2 = {"GMan's Awesome Car Company", "The future", "The best"}; CarList *head = createNode(car1); // create the first node head-&gt;next = createNode(car2); // now traverse the list CarList *iter = head; for (; iter != 0; iter = iter-&gt;next) { // output, dereference iterator to get the actual node std::cout &lt;&lt; "Car: " &lt;&lt; *iter &lt;&lt; std::endl; } // dont forget to delete! iter = head; while (iter) { // store next CarList *next = iter-&gt;next; // delete and move on delete iter; iter = next; } } </code></pre> <p>Now, if you don't have to create your own linked list, use the standard link list instead, it simplifies your task immensely:</p> <pre><code>#include &lt;algorithm&gt; #include &lt;iostream&gt; #include &lt;iterator&gt; #include &lt;list&gt; #include &lt;string&gt; using namespace std; struct Car { string maker; string year; string model; }; // this is the output function for a car std::ostream&amp; operator&lt;&lt;(std::ostream&amp; sink, const Car&amp; car) { // print out car info sink &lt;&lt; "Make: " &lt;&lt; car.maker &lt;&lt; "\nYear: " &lt;&lt; car.year &lt;&lt; "\nModel: " &lt;&lt; car.model &lt;&lt; std::endl; return sink; } int main(void) { // a car list typedef std::list&lt;Car&gt; CarList; // a couple cars Car car1 = {"Dodge", "2001", "Stratus"}; Car car2 = {"GMan's Awesome Car Company", "The future", "The best"}; CarList cars; cars.push_back(car1); cars.push_back(car2); // now traverse the list (copy to ostream) std::copy(cars.begin(), cars.end(), std::ostream_iterator&lt;Car&gt;(std::cout,"\n")); // delete done automatically in destructor } </code></pre> <p>Hope this helps.</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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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