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

grayloach1 2005-04-29 05:37:12

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

...全文
94 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
grayloach1 2005-04-29
  • 打赏
  • 举报
回复
天那,我检查这么多遍,居然是忘记了#include<string>,我还以为我早加了呢。
谢谢!
lw1a2 2005-04-29
  • 打赏
  • 举报
回复
在名字空间外使用名字空间里的东西,要加域操作符

namespace myName
{
int a;
}

myName::a=0;
xuzheng318 2005-04-29
  • 打赏
  • 举报
回复
using namespace std;
qhfu 2005-04-29
  • 打赏
  • 举报
回复
using std::string;
或者
using namespace std;
或者直接 std::string;
qhfu 2005-04-29
  • 打赏
  • 举报
回复
#include<string>
lw1a2 2005-04-29
  • 打赏
  • 举报
回复
1.
#include <string>
using std::string;
2.
namespace myName
{
.....
}

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧