那位高手帮我看一下重载《错在那里?
古布 2004-03-05 10:15:10 // List.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using std::cerr;
using std::cout;
using std::ostream;
class List {
friend ostream& operator<<( ostream & os, const List & );
public:
List();
~List();
//List( const List& );
void insert_front( int value );
bool is_empty() const { return front == 0; }
private:
class ListItem {
friend insert_front( int value );
public:
ListItem( int val );// : value( val ), next( 0 ) { }
int value;
ListItem *next;
};
ListItem *front;
ListItem *end;
//list_Item *current;
//int size;
};
friend ostream& operator<<( ostream & os, const List & lc)
{
List::ListItem *pt = 0;
for ( pt = lc.front; pt; pt = pt->next )
os << *pt;
}
List::List() : front( 0 ), end( 0 ) {}
void List::insert_front( int value )
{
ListItem *pt = new ListItem( value );
if ( !is_empty()) {
front = end = pt;
}
else {
pt = front;
front = pt;
}
}
friend ostream& operator<<( ostream& os, const List::ListItem & ic )
{
os << ic.value << "next->";
}
List::ListItem::ListItem( int val )
: value( val ), next( 0 ) { }
int main(int argc, char* argv[])
{
List t1;
t1.insert_front( 3 );
t1.insert_front( 4 );
return 0;
}