插入运算符和命名空间 [英] insertion operator and namespace?
问题描述
亲爱的,
我在插入操作符和命名空间方面有一些问题。
我有一个头文件foo.h包含类的声明,typedef和
命名空间中typedef的插入运算符
namespace foo
{
class Foo
{
typedef std :: vector< unsigned char> Vec;
typedef std :: vector< Key> VecVec;
....
};
std :: ostream& operator<<(std :: ostream& os,const Foo :: Vec& v);
std :: ostream& operator<<(std :: ostream& os,const Foo :: VecVec& v);
}
然后文件foo.cc中的定义
名称空间foo
{
std :: ostream& operator<<(std :: ostream& os,const Foo :: Vec& v)
{
...
}
std :: ostream& operator<<(std :: ostream& os,const Foo :: VecVec& v)
{
}
}
现在我想在程序中使用插入运算符。如果
程序看起来像
#include" foo.h"
int main()
{
Foo :: Vec vec;
Foo :: VecVec vecs;
.. ..
//使用命名空间需要指令
//否则编译器会与
混淆// std中的其他可用插入::
{
使用namespace foo;
std :: cout<< vec<< std :: endl;
std :: cout<< vecs<< std :: endl;
}
}
我想知道如何避免使用using namespace指令?
此致,
Patrick
Dear all,
I have some problem with insertion operator together with namespace.
I have a header file foo.h containing declaration of classes, typedefs and
insertion operators for the typedefs in a named namespace
namespace foo
{
class Foo
{
typedef std::vector<unsigned char> Vec;
typedef std::vector<Key> VecVec;
....
};
std::ostream & operator<<(std::ostream &os, const Foo::Vec &v);
std::ostream & operator<<(std::ostream &os, const Foo::VecVec &v);
}
Then the definition in the file foo.cc
namespace foo
{
std::ostream & operator<<(std::ostream &os, const Foo::Vec &v)
{
...
}
std::ostream & operator<<(std::ostream &os, const Foo::VecVec &v)
{
}
}
Now I would like to use the insertion operators in a programm. If the
program looks like
#include "foo.h"
int main()
{
Foo::Vec vec;
Foo::VecVec vecs;
....
// "using namespace" directive required
// otherwise compiler get confused with
// other available insertions in std::
{
using namespace foo;
std::cout << vec << std::endl;
std::cout << vecs << std::endl;
}
}
I wonder how to avoid using the using namespace directive?
Sincerely,
Patrick
推荐答案
2005年11月8日星期二21:40:47 +0100,Patrick Guio< pa ****** @ ii.uib.no>
写道:
On Tue, 8 Nov 2005 21:40:47 +0100, Patrick Guio <pa******@ii.uib.no>
wrote:
插件操作符和命名空间有一些问题。
我有一个头文件foo.h,包含类声明,typedef和
插入操作符命名空间中的typedef
命名空间foo
{F>
{
typedef std :: vector< unsigned char> Vec;
typedef std :: vector< Key> VecVec;
....
};
std :: ostream& operator<<(std :: ostream& os,const Foo :: Vec& v);
std :: ostream& operator<<(std :: ostream& os,const Foo :: VecVec& v);
}
然后文件foo.cc中的定义
命名空间foo
{
std :: ostream& operator<<(std :: ostream& os,const Foo :: Vec& v)
{
...
}
std :: ostream& operator<<(std :: ostream& os,const Foo :: VecVec& v)
{
}
}
现在我想在程序中使用插入运算符。如果
程序看起来像
#include" foo.h"
int main()
{
Foo :: Vec vec;
Foo :: VecVec vecs;
....
//" using namespace"指令要求
//否则编译器与st / :: //其他可用的插入混淆std ::
{
使用命名空间foo;
std :: cout<< ; vec<< std :: endl;
std :: cout<< vecs<< std :: endl;
}
我想知道如何避免使用using namespace指令?
你必须把运算符的重载<<进入全球
命名空间,即:
std :: ostream& operator<<(std :: ostream& os
,const foo :: Foo :: Vec& v)
{
...
}
std :: ostream& operator<<(std :: ostream& os
,const foo :: Foo :: VecVec& v)
{
}
我不明白为什么你之前的声明在没有指定命名空间的情况下编译
,即:
#包括foo.h
int main()
{F> :: Vec vec;
Foo :: VecVec vecs;
Dear all,
I have some problem with insertion operator together with namespace.
I have a header file foo.h containing declaration of classes, typedefs and
insertion operators for the typedefs in a named namespace
namespace foo
{
class Foo
{
typedef std::vector<unsigned char> Vec;
typedef std::vector<Key> VecVec;
....
};
std::ostream & operator<<(std::ostream &os, const Foo::Vec &v);
std::ostream & operator<<(std::ostream &os, const Foo::VecVec &v);
}
Then the definition in the file foo.cc
namespace foo
{
std::ostream & operator<<(std::ostream &os, const Foo::Vec &v)
{
...
}
std::ostream & operator<<(std::ostream &os, const Foo::VecVec &v)
{
}
}
Now I would like to use the insertion operators in a programm. If the
program looks like
#include "foo.h"
int main()
{
Foo::Vec vec;
Foo::VecVec vecs;
....
// "using namespace" directive required
// otherwise compiler get confused with
// other available insertions in std::
{
using namespace foo;
std::cout << vec << std::endl;
std::cout << vecs << std::endl;
}
}
I wonder how to avoid using the using namespace directive?
You have to put the overloads for operator<< into the global
namespace, i.e.:
std::ostream & operator<<(std::ostream &os
, const foo::Foo::Vec &v)
{
...
}
std::ostream & operator<<(std::ostream &os
, const foo::Foo::VecVec &v)
{
}
What I don''t understand is why your earlier declarations compile
without specifying the namespace, i.e.:
#include "foo.h"
int main()
{
Foo::Vec vec;
Foo::VecVec vecs;
如果没有using指令,使用声明,
或明确指定名称空间限定符,这不应该编译。
-
Bob Hairgrove
否******* ***@Home.com
> #include" foo.h"
> #include "foo.h"
int main()
{F> :: Vec vec;
Foo :: VecVec vecs;
怎么编译? class Foo是在
foo.h中的命名空间foo中定义的,它不是当前命名空间的一部分?
....
//使用命名空间指令要求
//否则编译器与st / :: //其他可用的插入混淆std ::
{
使用命名空间foo;
std :: cout<< ; vec<< std :: endl;
std :: cout<< vecs<< std :: endl;
}
}
int main()
{
Foo::Vec vec;
Foo::VecVec vecs;
how does that compile? class Foo is defined inside a namespace foo in
foo.h, which is not part of the current namespace?
....
// "using namespace" directive required
// otherwise compiler get confused with
// other available insertions in std::
{
using namespace foo;
std::cout << vec << std::endl;
std::cout << vecs << std::endl;
}
}
使用<<对于类型的运算符重载需要
argument-dependend lookup,因为相关运算符不是在可见命名空间中定义的
。遗憾的是,标准似乎没有为typedef定义参数依赖查找,查询仍然是与具体类型相关联的
(在你的情况下是std :: vector) )。
您可以直接调用操作员以明确指定
命名空间,尽管您可能不想这样做:
(foo :: operator<<(std :: cout,vec))<< std :: endl;
(foo :: operator<<(std :: cout,vecs))<< std :: endl;
也许你应该在全局
命名空间中定义运算符重载。
- 彼得
using the << operator overload for you types requires
argument-dependend lookup because the concerning operator is not
defined in a visible namespace. unfortunately the standard doesn''t seem
to define argument-dependend lookup for typedefs, the lookup is still
associated to the concrete type (std::vector in your case).
you could call the operator directly to be able to specify the
namespace explicitely, though you probably wouldn''t want to do that:
(foo::operator << (std::cout, vec)) << std::endl;
(foo::operator << (std::cout, vecs)) << std::endl;
maybe you should just define the operator overloads in the global
namespace.
-- peter
2005年11月8日星期二,Bob Hairgrove写道:
嗨Bob,
我仍有问题可以在下面的代码中总结:
On Tue, 8 Nov 2005, Bob Hairgrove wrote:
Hi Bob,
I still have problem which can be summarize in the following code:
你必须为运算符<<<<进入全局
命名空间,即:
std :: ostream& operator<<(std :: ostream& os
,const foo :: Foo :: Vec& v)
{
...
}
std :: ostream& operator<<(std :: ostream& os
,const foo :: Foo :: VecVec& v)
{
}
我不喜欢什么不明白为什么你的早期声明在没有指定命名空间的情况下编译
,即:
You have to put the overloads for operator<< into the global
namespace, i.e.:
std::ostream & operator<<(std::ostream &os
, const foo::Foo::Vec &v)
{
...
}
std::ostream & operator<<(std::ostream &os
, const foo::Foo::VecVec &v)
{
}
What I don''t understand is why your earlier declarations compile
without specifying the namespace, i.e.:
#include" foo.h"
int main()
{Foo :: Vec vec;
Foo :: VecVec vecs;
#include "foo.h"
int main()
{
Foo::Vec vec;
Foo::VecVec vecs;
如果没有using指令,这不应该编译,使用声明,
或明确指定命名空间限定符。
This shouldn''t compile without a using directive, a using declaration,
or specifying the namespace qualifier explicitly.
我的错,这只是我用来描绘问题的代码片段。
当然应该foo :: Foo :: Vec和foo :: Foo :: VecVec(或者之前使用foo :: Foo"指令的
)。
无论如何,我仍然在下面的代码中有2个编译问题,
Fred在命名空间fred中的类。
编译器( g ++ v.4.0.1)抱怨
*''int fred :: Fred :: i_''在上下文中是私有的''return o<< fred.i_;''
*''std :: cout<< [snip]<<弗雷德<< \ n" ;;''是一个模棱两可的超载
,候选人是:
std :: ostream& operator<<(std :: ostream&,const fred :: Fred&)
std :: ostream& fred :: operator<<(std :: ostream&,const fred :: Fred&)
如何解决这些问题呢?
此致,
Patrick
#include< iostream>
//弗雷德班命名空间
名称空间fred
{
class Fred
{
public:
朋友std :: ostream&运营商LT;< (std :: ostream& o,const fred :: Fred& fred);
Fred():i_(1){}
~Fred(){}
私人:
int i_;
};
}
std :: ostream&运营商LT;< (std :: ostream& o,const fred :: Fred& fred)
{
return o<< fred.i_;
}
int main()
{
fred :: Fred fred;
std :: cout<< 我的弗雷德对象: <<弗雷德<< " \ n";
返回0;
}
My fault, this is just a code snippet I made to sketch my problem. Of
course there should foo::Foo::Vec and foo::Foo::VecVec (or a
"using foo::Foo" directive before).
Anyway I still have 2 compilation problems in the following code with
the class Fred in namespace fred.
The compiler (g++ v.4.0.1) complains that
* ''int fred::Fred::i_'' is private within the context ''return o << fred.i_;''
* ''std::cout << [snip] << fred << "\n";'' is an ambiguous overload
and candidates are:
std::ostream& operator<<(std::ostream&, const fred::Fred&)
std::ostream& fred::operator<<(std::ostream&, const fred::Fred&)
Any idea of how to solve these problems?
Sincerely,
Patrick
#include <iostream>
// class in fred namespace
namespace fred
{
class Fred
{
public:
friend std::ostream& operator<< (std::ostream& o, const fred::Fred& fred);
Fred() : i_(1) {}
~Fred() {}
private:
int i_;
};
}
std::ostream& operator<< (std::ostream& o, const fred::Fred& fred)
{
return o << fred.i_;
}
int main()
{
fred::Fred fred;
std::cout << "My Fred object: " << fred << "\n";
return 0;
}
这篇关于插入运算符和命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!