std命名空间的问题;cout输出classname的问题;

grayloach1 grayloach1 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

#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等函数?可否示范一下啊。

...全文
评论 6
收藏 已收藏
分享
返回顶部
grayloach1
C++ 语言 65,214 成员 250,514 内容