c++中名字空间的问题

wuruogeng 2009-08-02 11:49:21
最近学习C++名字空间看的有点迷糊了。。做了一个实验:
#include<iostream>
using namespace std;


namespace abcd
{
using namespace std;
class X
{
int x;
int xx;
public:
X(int a,int b){cout<<"X(int,int) called"<<endl;x = a; xx = b; }//编译时的时候提示找不到cout关键字。。感觉是cout是在全局的名字空间吧。所以加了上面红色的那一句。但是还是不行。
~X(cout<<"~X called"<<endl;)
X(){cout<<"X called"<<endl;x = 0; xx = 0;}
void set(int aa,int bb){x = aa; xx = bb;}
};
int abcde;

}


int main()
{
using namespace abcd;
abcd::X ax;
return 0;
}

最后还想问一句。。以前看过一句话
就是包含头文件的时候
#include<iostream>
using namespace std;
文件iostream不用加.h----是因为使用了标准的名字空间:using namespace std;原因。这个也没搞明白,期待高手解答。
...全文
189 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuruogeng 2009-08-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 loaden 的回复:]
#include <iostream> 是新标准库
#include <iostream.h> 老的,不建议用,将来要废弃的。

using namespace std;
放在class X里面试试。
[/Quote]

恩,了解了,还想问一下,为什么使用老的标准库 iostream.h就不用使用标准的名字空间了,不用using namespace std;?
是不是新的标准库iostream是在标准名字空间std;下的。而老的不是。。
另外,就是包含有文件,怎么能不用.h呢。很疑惑。。
wuruogeng 2009-08-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 mstlq 的回复:]
其实问题出在析构函数的定义上……
请参考下面可行的代码^_^
C/C++ code#include<iostream>usingnamespace std;namespace abcd
{usingnamespace std;class X
{int x;int xx;public:
X(int a,int b){
cout<<"X(int,int) called"<<endl;
x= a;
xx= b;
}~X(){cout<<"~X called"<<endl;};//请注意这一行 X(){
std::cout<<"X called"<<endl;
x=0;
xx=0;
}voidset(int aa,int bb){
x= aa;
xx= bb;
}
};int abcde;

}int main()
{return0;
}
[/Quote]

看到了,没仔细查。丢人了,呵呵。
还想问一下。。。名字空间是不是可以代替声明。。而且使用范围是可以选择的。如果声明的话。就是整个编译单元都可用了吧。。
lori227 2009-08-03
  • 打赏
  • 举报
回复
~X(){cout <<"~X called" <<endl;}
wuruogeng 2009-08-03
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 yshuise 的回复:]
C/C++ code#include"stdafx.h"
#include<iostream>usingnamespace std;namespace abcd
{//不需要全部引入名词空间 using namespace std;using std::cout;using std::endl;class X
{int x;int xx;public:
X(int a,in¡­
[/Quote]

呵呵,见笑了。。
yshuise 2009-08-03
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>
using namespace std;
namespace abcd
{
//不需要全部引入名词空间 using namespace std;
using std::cout;
using std::endl;

class X
{
int x;
int xx;
public:
X(int a,int b){cout <<"X(int,int) called" <<endl;x = a; xx = b; }
~X(){cout <<"~X called" <<endl;}//是"{ }",不是"()"
X(){cout <<"X called" <<endl;x = 0; xx = 0;}
void set(int aa,int bb){x = aa; xx = bb;}
};
int abcde;

}

int _tmain(int argc, _TCHAR* argv[])
{

using namespace abcd;
abcd::X ax;
return 0;
}
yshuise 2009-08-03
  • 打赏
  • 举报
回复
哪有这么用的?
cxxer 2009-08-03
  • 打赏
  • 举报
回复
名字空间让名字更乱,
ysysbaobei 2009-08-03
  • 打赏
  • 举报
回复
顶下
wanghao111 2009-08-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wuruogeng 的回复:]
恩,了解了,还想问一下,为什么使用老的标准库 iostream.h就不用使用标准的名字空间了,不用using namespace std;?
是不是新的标准库iostream是在标准名字空间std;下的。而老的不是。。
另外,就是包含有文件,怎么能不用.h呢。很疑惑。。
[/Quote]
iostream.h没有命名空间,iostream有命名空间
飞天御剑流 2009-08-03
  • 打赏
  • 举报
回复
iostream.h是引入命名空间之前的头文件,iostream引入命名空间之后的,把所有的关键字和标准库名字都包含在命名空间std里面了,还有从c库来的c开头的库,也在里面。
lgw26046044 2009-08-03
  • 打赏
  • 举报
回复
并没有错只是你的代码有些地方写的并不规范而已,下面还是你的代码,现在就没有问题了,可以通过编译了
有一点,你要注意,就是你是不是用的编译器是VC6.0,如果是的话,那么可能是编译器的问题因为VC6.0对命名空间的支持并不好,我以前做过一些实验就是同样一段代码在vc6.0中会编译出错而在DEV-c++就可以正常通过编译,不信你也可以实验一下(实验代码在最后面)。
#include <iostream>
using namespace std;
namespace abcd
{
class X
{
int x;
int xx;
public:
X(int a,int b){cout <<"X(int,int) called" <<endl;x = a; xx = b; }
~X(){cout <<"~X called" <<endl;}
X(){cout <<"X called" <<endl;x = 0; xx = 0;}
void set(int aa,int bb){x = aa; xx = bb;}
};
int abcde;

}


int main(void)
{
abcd::X ax;
system("pause");
return 0;
}


实验代码如下:


#include<iostream>
using namespace std;
class Point
{
public:
Point(int a,int b):x(a),y(b){}
void Show();
private:
int x;
int y;
};
void Point::Show()
{
cout<<"0\t1\t2\t3\t4\t5\t6\t7\t8\t9"<<endl;
for(int i=1;i<10;i++)
{
cout<<i<<endl;
if(y==i)
for(int j=1;j<10;j++)
if(x==j)
{
cout<<"*";break;
}
else
cout<<" \t";
cout<<endl;
}
}

namespace ns1
{
class A
{
public:
A(int e):a(e){}
int Geta()
{return a;}
void ShowSqrta()
{
cout<<"a*a="<<a*a<<endl;
}
private:
int a;
};
}

namespace ns2
{
class A
{
public:
A(int e):b(e){}
int Getb()
{return b;}
void ShowSqrtb()
{cout<<"b*b="<<b*b<<endl;}
private:
int b;
};
}


class B:public ns1::A,public ns2::A
{
public:
B(int a,int b):ns1::A(a),ns2::A(b){}
void ShowSqrt()
{
ns1::A::ShowSqrta();
ns2::A::ShowSqrtb();
}
operator Point()
{
return Point(Geta(),Getb());
}
};

int main()
{
B b(3,4);
b.ShowSqrt();
Point(b).Show();
system("pause");
return 0;
}

//以上代码在VC6.0中编译会出错而在DEV-c++中编译就不会出错,你可以试一试。

mstlq 2009-08-02
  • 打赏
  • 举报
回复
其实问题出在析构函数的定义上……
请参考下面可行的代码^_^
#include <iostream>

using namespace std;
namespace abcd
{

using namespace std;

class X
{
int x;
int xx;

public:
X(int a,int b){
cout <<"X(int,int) called" <<endl;
x = a;
xx = b;
}

~X(){cout <<"~X called" <<endl;};//请注意这一行
X(){
std::cout <<"X called" <<endl;
x = 0;
xx = 0;
}

void set(int aa,int bb){
x = aa;
xx = bb;
}
};

int abcde;

}

int main()
{

return 0;
}
老邓 2009-08-02
  • 打赏
  • 举报
回复
#include <iostream> 是新标准库
#include <iostream.h> 老的,不建议用,将来要废弃的。

using namespace std;
放在class X里面试试。
陈后骏 2009-08-02
  • 打赏
  • 举报
回复
不是找不到cout, 是 << 中加了空格吧!

64,677

社区成员

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

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