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:
#include <iostream>
#include <string>
using namespace std; // not recommended, but useful
// in snippets
// T is usually used, but this is of course up to you
template <class T>
struct Node
{
typedef T value_type; // a usual typedef
value_type info;
Node<value_type> *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 <typename T>
Node<T> *createNode(const T& info = T())
{
// allocate node
Node<T> *result = new Node<T>;
result->info = info;
result->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 <typename T>
std::ostream& operator<<(std::ostream& sink, const Node<T>& node)
{
// note that we cannot assume what node contains!
// rather, stream the info attached to the node
// to the ostream:
sink << node.info;
return sink;
}
// this is the output function for a car
std::ostream& operator<<(std::ostream& sink, const Car& car)
{
// print out car info
sink << "Make: " << car.maker <<
"\nYear: " << car.year <<
"\nModel: " << car.model << std::endl;
return sink;
}
int main(void)
{
// a car list
typedef Node<Car> 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->next = createNode(car2);
// now traverse the list
CarList *iter = head;
for (; iter != 0; iter = iter->next)
{
// output, dereference iterator to get the actual node
std::cout << "Car: " << *iter << std::endl;
}
// dont forget to delete!
iter = head;
while (iter)
{
// store next
CarList *next = iter->next;
// delete and move on
delete iter;
iter = next;
}
}
Now, if you don't have to create your own linked list, use the standard link list instead, it simplifies your task immensely:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <string>
using namespace std;
struct Car
{
string maker;
string year;
string model;
};
// this is the output function for a car
std::ostream& operator<<(std::ostream& sink, const Car& car)
{
// print out car info
sink << "Make: " << car.maker <<
"\nYear: " << car.year <<
"\nModel: " << car.model << std::endl;
return sink;
}
int main(void)
{
// a car list
typedef std::list<Car> 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<Car>(std::cout,"\n"));
// delete done automatically in destructor
}
Hope this helps.