PDA

View Full Version : using namespace std



nuilua3
02-03-2005, 19:38
Chào mọi người.Mình học khônghiểu câu lệnh "using namespace std"trong C++dùng để làm gì.Giải thích cho mình với.

fantasy_dreams
03-03-2005, 11:21
using namespace std;

dùng để khai báo sử dụng namespace std của bộ thư viện STL.

bomayday123
16-02-2009, 13:02
cho mình hỏi tí :sao may mình lại ko chạy dc lệnh using namespace trong devC++ 4.92(máy mình dùng chip AMD ) ai biết giúp mình với

ptaminh
16-02-2009, 15:11
cho mình hỏi tí :sao may mình lại ko chạy dc lệnh using namespace trong devC++ 4.92(máy mình dùng chip AMD ) ai biết giúp mình với

dùng chip AMD thì đâu có liên quan gì đến using namespace đâu nhỉ.

Mình thì chưa xài devC++ bao giờ, có những trình biên dịch khác nhau thì có thể khác nhau một chút.

Hok bik zì
16-02-2009, 15:19
cho mình hỏi tí :sao may mình lại ko chạy dc lệnh using namespace trong devC++ 4.92(máy mình dùng chip AMD ) ai biết giúp mình với

Không chạy đc là như thế nào ? Lỗi gì post lên đây, hỏi khơi khơi ai mà trả lời đc :blink:

lenguyenthanh
17-02-2009, 17:52
mình dùng dev c++ và chạy bt.Nói thêm (mặc dù ko liên quan) mình cũng là chip amd :D

phantanhiep
19-03-2010, 13:54
1/cai using namebase std, co dung hay khong cung k thanh van de trong C++ tren nen dev.
2/trong dev ban tao new project C++ la ok cho cac thu vien include jong nhu C.

nhocyeuhoc
03-05-2010, 09:51
em moi hoc C++ ! may anh cho em hoi tai sao phai dung using namespace std de lam gi a?

onminh
03-05-2010, 10:56
em moi hoc C++ ! may anh cho em hoi tai sao phai dung using namespace std de lam gi a?

Hãy hiểu nôm na như sau đã. Trong vs, nếu #include <iostream.h> (có .h) thì khỏi cần using namespace std mình vẫn cin, cou bình thường. Nhưng nếu #include <iostream> (không có .h) thì phải có using namespace std; Trong TC hay BC for DOS thì không có.
Bạn có thể xem thêm chú giải của MSDN 6.0:
using Directive

The using-directive allows the names in a namespace to be used without the namespace-name as an explicit qualifier. In contrast to a using declaration, which allows an individual name to be used without qualification, the using directive allows all the names in a namespace to be used without qualification. See using Declaration for more information.

Syntax

using-directive :

using namespace ::opt nested-name-specifieropt namespace-name

The intent of the using-directive is to allow unique, descriptive names to be used when declaring functions and variables, without requiring the complete name every time access to the functions or variables is needed. Of course, the complete, qualified name can still be used to retain clarity.

The unqualified names can be used from the point of the using directive on. If a namespace is extended after a using-directive is given, the additional members of the namespace can be used, without qualification, after the extended-namespace-definition. For example:

namespace M
{
int i;
}

using namespace M;

namespace N
{
int j;
double f() { return M::d; } // error: M::d does not yet exist
}

namespace M // namespace extension
{
double d;
}
// now M::d can be used

It is possible for a using-directive to introduce conflicting names when used in another namespace. For example:

namespace M
{
int i;
}

namespace N
{
int i;
using namespace M; // no error here
}
.
.
.
void f()
{
using namespace N;
i = 7; // error: ambiguous: M::i or N::i?
}

In this example, bringing M::i into namespace N does not hide the declaration of N::i, but instead creates an ambiguity when N::i is used. In this manner, the using-directive can easily introduce unintended ambiguities. Consider the following code fragment:

namespace D
{
int d1;
void f(int);
void f(char);
}

using namespace D;

int d1; // no conflict with D::d1

namespace E
{
int e;
void f(int);
}

namespace D // namespace extension
{
int d2;
using namespace E;
void f(int);
}

void f()
{
d1++; // error: ambiguous: ::d1 or D::d1?
::d1++; // ok
D::d1++; // ok
d2++; // ok: D::d2
e++; // ok: E::e
f(1); // error: ambiguous: D::f(int) or E::f(int)?
f('a'); // ok D::f(char)
}

When a variable is referenced after a using-directive, the local variable of the same name takes precedence over the one declared in the specified namespace. For example:

namespace N {
int data = 4;
}

void f(bool flag) {
int data = 0;

if (flag) {
using namespace N;

prinf(“data=%d\n”, data);
}
}

void main() {
f(true);
}

In the above code, the variable data referenced in the printf statement is the local variable initialized to 0, instead of the variable initialized in namespace N. The output is data=0 instead of data=4.

In the presence of namespace using-directives, the way qualified names are looked up is shown in the following example:

namespace A {
int flag = 0;
}

namespace B {
using namespace A;
}

namespace C {
using namespace A;
using namespace B;
}

void main() {
printf(“C::flag = %d\n”, C::flag);
}

The qualified name (C::flag) is resolved to (A::flag) due to the namespace using-directives in namespace C.