std命名空间的问题;cout输出classname的问题;
#include <iostream>
using std::cin; //问题2:不得已这么用,因为以后会使用到自定义的list类.
using std::cout;
using std::endl;
using std::ostream;
template<typename elemType>
class list_item {
public:
list_item( elemType value, list_item *item_to_link_to = 0 );
elemType value() const { return _value; }
void value(elemType new_value) { _value = new_value; }
list_item* next() { return _next; }
void next( list_item *link ) { _next = link; }
private:
elemType _value;
list_item *_next;
};
template<typename elemType>
inline
list_item<elemType>::
list_item( elemType value, list_item *item ) : _value( value )
{
if ( !item )
_next = 0;
else {
_next = item->_next; //把当前元素插入到 item元素之后。
item->_next = this;
}
}
int main()
{
list_item<string> it( "2" );
list_item<string> it2( "3" , &it );
cout << it.next() << " : " ;
//cout << it.value() << endl; //问题1:无法输出
return 0;
}
问题:
1. 当采用list_item<int>时,可以正常输出value();
当采用list_item< string >时,输出失败,应该怎么改??
2. 怎么样定义自己的命名空间,可使用自己定义的list类同时又使用iostream头文件里的cont等函数?可否示范一下啊。