命名空间麻烦 [英] namespace trouble

查看:54
本文介绍了命名空间麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在重写一个C ++框架,以便命名空间替换旧的前缀名称

。请考虑以下课程:


=== boundingbox.h ===

[...]

命名空间VART {

class Transform; //类转换的前向声明

类BoundingBox {

friend std :: ostream& operator<<(std :: ostream& output,const

BoundingBox& box);

public:

[...]

void变换(const变换& trans);

[...]

============= ==

1)我试过class VART :: Transform;在命名空间VART {之前

给了我编译错误。目前的声明好吗?是否有更好的方法?


2)输出运算符在命名空间VART中。可能在外面吗?

如果是这个框架的用户会更加舒适......


3)我得到了boundingbox.h:36 :错误:声明''void

VART :: BoundingBox :: Transform(const VART :: Transform&)''transform.h:24:

错误:更改在g ++上编译时,''转换''的含义来自''类VART ::转换''"

我不明白为什么VART :: BoundingBox :: Transform

与VART :: Transform冲突。在这里可以做些什么?

解决方案

Bruno de Oliveira Schneider写道:

大家好,

我正在重写一个C ++框架,以便命名空间替换旧的前缀名称。请考虑以下课程:

=== boundingbox.h ===
[...]
命名空间VART {
类转换; //前向声明类转换
类BoundingBox {
朋友std :: ostream& operator<<(std :: ostream& output,const
BoundingBox& box);
public:
[...]
void Transform(const Transform& trans);


一般都是一个不好的名字(tm)来命名你的功能和你的类型

相同。

[.. 。]
===============
1)我试过类VART :: Transform;在命名空间VART {之前哪个
给了我编译错误。目前的声明好吗?有更好的方法吗?


如果必须在命名空间中转发声明一个类,则打开

命名空间,声明该类,关闭命名空间。它应该没问题。

2)输出运算符在命名空间VART中。它可能在外面吗?
这个框架的用户如果是......就会变得更加舒适...


你可以在它前面添加''::''(意思是全局命名空间)。

3)我得到boundingbox.h:36:错误:声明''void
VART :: BoundingBox :: Transform(const VART :: Transform&) ''
transform.h:24:错误:从''类改变''转换''的含义
VART :: Transform''"在g ++上编译时。我不明白为什么
VART :: BoundingBox :: Transform与VART :: Transform冲突。什么
可以在这里完成?




您可以(a)重命名您的功能或(b)详细说明
$ b $的名称b类型:


void变换(const类变换&);


但我会先尝试做(a)。 />

V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


Victor Bazarov写道:

这通常是一个不好的IDEA(tm)为您的功能和类型命名
相同。


你能详细说明吗?我真的不明白为什么

VART :: BoundingBox ::转换与VART :: Transform冲突。

如果你必须在名称空间中转发声明一个类,打开
命名空间,声明该类,关闭命名空间。应该没问题。


然后我想我的方式(打开命名空间一次,转发声明

转换并声明BoundingBox)比打开命名空间更好

两次(一次用于前向声明,然后用于类

声明),对吧?

你可以在前面添加''::''(含义在全局命名空间中。)




我该怎么做?我试过了:

===开始代码===

命名空间VART

{

class Transform; < br $>
类BoundingBox {

朋友std :: ostream& :: operator<<(std :: ostream& output,const

BoundingBox& box);

public:

[... ]

===结束代码===

但是g ++给了我:

错误:''std :: ostream&运算符<<(std :: ostream&,const

VART :: BoundingBox&)''应该在'''''中声明


Bruno de Oliveira Schneider写道:

Victor Bazarov写道:

它通常是一个不好的名称(tm)来命名你的功能和你的类型
相同。



你能详细说明吗?我真的不明白为什么
VART :: BoundingBox :: Transform与VART :: Transform冲突。




如果你试着参考''变换'内部''VART :: BoundingBox''类,

''变换''会是什么?那很明显吗?我不是说你不能这样做。
。我说你*不应该*。

如果你必须在命名空间中转发声明一个类,你打开
namespace,声明类,关闭命名空间。它应该很好。



然后我猜我做的方式(打开命名空间一次,转发声明
转换并声明BoundingBox)比打开更好
命名空间两次(一次用于前向声明然后用于类
声明),对吧?




对。

你可以在前面添加''::''(意思是在全局命名空间中)。



我该怎么做?我试过了:
===开始代码===
命名空间VART
{类转换;
类BoundingBox {
朋友std :: ostream& :: operator<<(std :: ostream& output,const
BoundingBox& box);
public:
[...]
===结束代码== =
但是g ++给了我:
错误:''std :: ostream&运算符<<(std :: ostream&,const
VART :: BoundingBox&)''应该在''::''中声明




因此,编译器会告诉您,您指的是没有声明

的函数。所以声明它。


V

-

请在通过电子邮件回复时删除资金'A'

我没有回复最热门的回复,请不要问


Hello all,

I''m rewriting a C++ framework, so that old prefixed names are replaced
by namespaces. Please consider the following class:

=== boundingbox.h ===
[...]
namespace VART {
class Transform; // forward declaration of class Transform
class BoundingBox {
friend std::ostream& operator<<(std::ostream& output, const
BoundingBox& box);
public:
[...]
void Transform(const Transform& trans);
[...]
===============
1) I tried "class VART::Transform;" before "namespace VART {" which
gave me compile errors. Is the current declaration ok? Is there a
better way?

2) The output operator is inside namespace VART. Could it be outside?
Users of this framework would fell more confortable if it was...

3) I get "boundingbox.h:36: error: declaration of ''void
VART::BoundingBox::Transform(const VART::Transform&)'' transform.h:24:
error: changes meaning of ''Transform'' from ''class VART::Transform''"
when compiling on g++. I don''t see why VART::BoundingBox::Transform
conflicts with VART::Transform. What could be done here?

解决方案

Bruno de Oliveira Schneider wrote:

Hello all,

I''m rewriting a C++ framework, so that old prefixed names are replaced
by namespaces. Please consider the following class:

=== boundingbox.h ===
[...]
namespace VART {
class Transform; // forward declaration of class Transform
class BoundingBox {
friend std::ostream& operator<<(std::ostream& output, const
BoundingBox& box);
public:
[...]
void Transform(const Transform& trans);
It is generally a BAD IDEA(tm) to name your functions and your types
the same.
[...]
===============
1) I tried "class VART::Transform;" before "namespace VART {" which
gave me compile errors. Is the current declaration ok? Is there a
better way?
If you have to forward-declare a class in a namespace, you open the
namespace, declare the class, close the namespace. It should be fine.
2) The output operator is inside namespace VART. Could it be outside?
Users of this framework would fell more confortable if it was...
You could prefix it with ''::'' (meaning in the global namespace).
3) I get "boundingbox.h:36: error: declaration of ''void
VART::BoundingBox::Transform(const VART::Transform&)''
transform.h:24: error: changes meaning of ''Transform'' from ''class
VART::Transform''" when compiling on g++. I don''t see why
VART::BoundingBox::Transform conflicts with VART::Transform. What
could be done here?



You could (a) rename your function or (b) elaborate the name of the
type:

void Transform(const class Transform&);

But I''d try doing (a) first.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Victor Bazarov wrote:

It is generally a BAD IDEA(tm) to name your functions and your types
the same.
Could you elaborate on that? I really don''t see why
VART::BoundingBox::Transform conflicts with VART::Transform.
If you have to forward-declare a class in a namespace, you open the
namespace, declare the class, close the namespace. It should be fine.
Then I guess the way I did (open the namespace once, forward-declare
Transform and declare BoundingBox) is better than opening the namespace
twice (once for forward declarations and then for the class
declaration), right?
You could prefix it with ''::'' (meaning in the global namespace).



How do I do that? I tried:
=== begin code ===
namespace VART
{
class Transform;
class BoundingBox {
friend std::ostream& ::operator<<(std::ostream& output, const
BoundingBox& box);
public:
[...]
=== end code ===
But g++ gives me:
error: ''std::ostream& operator<<(std::ostream&, const
VART::BoundingBox&)'' should have been declared inside ''::''


Bruno de Oliveira Schneider wrote:

Victor Bazarov wrote:

It is generally a BAD IDEA(tm) to name your functions and your types
the same.



Could you elaborate on that? I really don''t see why
VART::BoundingBox::Transform conflicts with VART::Transform.



If you try referring to ''Transform'' inside ''VART::BoundingBox'' class,
which ''Transform'' would that be? Is that obvious? I am not saying
that you *can''t* do that. I am saying that you *shouldn''t*.

If you have to forward-declare a class in a namespace, you open the
namespace, declare the class, close the namespace. It should be
fine.



Then I guess the way I did (open the namespace once, forward-declare
Transform and declare BoundingBox) is better than opening the
namespace twice (once for forward declarations and then for the class
declaration), right?



Right.

You could prefix it with ''::'' (meaning in the global namespace).



How do I do that? I tried:
=== begin code ===
namespace VART
{
class Transform;
class BoundingBox {
friend std::ostream& ::operator<<(std::ostream& output, const
BoundingBox& box);
public:
[...]
=== end code ===
But g++ gives me:
error: ''std::ostream& operator<<(std::ostream&, const
VART::BoundingBox&)'' should have been declared inside ''::''



So, the compiler tells you that you''re referring to a function that has
not been declared. So declare it.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于命名空间麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆