名称空间和函数重载 [英] namespaces and function overloading

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

问题描述

大家好,


我有以下代码:


命名空间A {

inline void func (int){...; }

inline void func(float){...; }

inline void func(char){...; }

}


命名空间B {

inline void func(double){...; }


内联myApp()

{

int a;

func(a); //错误在这里

}

}


导致编译错误:

无法将参数1从''int''转换为''double''...

我猜这意味着命名空间A中func的所有定义都是

在命名空间B中被遗忘。这对于c ++来说是一般的吗?或者它是特定于我的microsoft visual c ++ 6.0的
。更重要的是,是否有一个

解决方案/解决方案?

提前谢谢,

Richard

Hi all,

I have the following code:

namespace A {
inline void func(int) { ...; }
inline void func(float) { ...; }
inline void func(char) { ...; }
}

namespace B {
inline void func(double) { ...; }

inline myApp()
{
int a;
func(a); // error here
}
}

Which results in the compile-error:
"cannot convert parameter 1 from ''int'' to ''double'' ...

Which I guess means that all the definitions of func in namespace A are
forgotten in namespace B. Is this something general for c++ or is it
specific for my microsoft visual c++ 6.0 . And more importantly, is there a
work-around / solution ?
thanks ahead,
Richard

推荐答案

RA Scheltema写道:
RA Scheltema wrote:

大家好,

我有以下代码:

namespace A {
inline void func(int){...; }
内联void func(float){...; }
inline void func(char){...;命名空间B {
inline void func(double){...;内联myApp()
{
int a;
func(a); //错误这里
}

导致编译错误:
无法将参数1从''int''转换为''double ''...


嗯。编译上面的内容时,我不会收到此错误。我使用VC ++ 6.0



我想这意味着名称空间A中func的所有定义都被遗忘在命名空间B中。

Hi all,

I have the following code:

namespace A {
inline void func(int) { ...; }
inline void func(float) { ...; }
inline void func(char) { ...; }
}

namespace B {
inline void func(double) { ...; }

inline myApp()
{
int a;
func(a); // error here
}
}

Which results in the compile-error:
"cannot convert parameter 1 from ''int'' to ''double'' ...
Hmm. I don''t get this error when compiling the above. And I
use VC++ 6.0

Which I guess means that all the definitions of func in namespace A are
forgotten in namespace B.




不,他们不会被遗忘。但是没有搜索命名空间A

函数


-

Karl Heinz Buchegger
kb ****** @ gascad.at


>嗯。编译上面的内容时,我不会收到此错误。我
> Hmm. I don''t get this error when compiling the above. And I
使用VC ++ 6.0


嗯,在我的项目中它是相当大的代码库的一部分。也许我应该有

检查代码:(但是这个例子是一个非常精简的版本

我的问题。

不,他们但是没有忘记。但命名空间A没有搜索
函数
use VC++ 6.0
Hmm, in my project its part of quite a large code-base. Maybe I should have
checked the code :(, however the example is a very stripped down version of
my problem.
No, they are not forgotten. But namespace A is not searched for the
functions




但这意味着当在命名空间B中找到函数func时它将

只尝试这个并且不打扰命名空间A中的那些?这将是

与我在项目中发现的内容一致,因为错误不会发生

当我没有在命名空间B中定义函数func时(在我的项目中是

....)。 />

我找到了以下解决方案:


使用A :: func;

命名空间B {

....

}

然而这会导致错误:

模糊调用func ... ;


和显式转换无法解决问题。

有什么我做错了吗?

问候,

Richard



But this means that when a function "func" is found in namespace B it will
only try this one and don''t bother with the ones in namespace A ? This would
concur with what I''m finding in my project, since the error does not occur
when I don''t define a function "func" in namespace B (in my project that is
....).

I did find the following solution:

using A::func;
namespace B{
....
}
however this results in the error:
"ambiguous call to func ..."

and explicit casting does not solve the problem.
Is there something I''m doing wrong ?
regards,
Richard


>>嗯。我没有得到这个错误?
>> Hmm. I don''t get this error when ?
编译上面的内容。而且我使用VC ++ 6.0
compiling the above. And I
use VC++ 6.0


嗯,在我的项目中它是一个相当大的代码库。也许我应该检查代码:(但是示例
是一个非常精简的版本
我的问题。


Karl是什么意思从int

到double的转换是合法的,你不应该

有关它的错误。可能是你的

错误在真实代码上有所不同,我认为我们都明白你的意思。

不,它们不会被遗忘。但命名空间
A不是搜索函数
但这意味着当在命名空间B中找到一个函数
func时,它将只尝试这个函数并且不用打扰
命名空间A中的那些?这将与我在我的
项目中找到的内容一致,因为当我没有定义函数func时,错误不会发生。在命名空间B中(在我的项目中是
...)。


这个:


命名空间A

{

void f();

}


n amespace B

{

void g()

{

f(); //错误

}

}


无效。命名空间意味着
防止名称冲突,因此,设计命名空间中的函数是

不能从其他人那里获得,没有特殊的

语法。

我找到了以下解决方案:
使用A :: func;
命名空间B {
...

}


这是一种方法。

然而这会导致错误:
暧昧地打电话给func ......


在哪里?如果它在命名空间范围内,这是很奇怪的,因为在命名空间中,本地名称

将始终优先于其他名称。

因此,这是有效的:


名称空间A

{

void f(int){}

}


使用命名空间A;

命名空间B

{

void f( int){}


void g()

{

f(10); //好的,调用B :: f()

}

}


问题出在全球范围内:


int main()

{

使用命名空间A;

使用命名空间B;


f(10); //不明确

}

和显式转换无法解决
问题。


什么演员?

有什么我做错了吗?

Hmm, in my project its part of quite a
large code-base. Maybe I should have
checked the code :(, however the example
is a very stripped down version of
my problem.
What Karl meant is that conversion from int
to double is legal and you shouldn''t have
had an error about it. It may be that your
error was different on the real code, I
think we all understand what you mean.
No, they are not forgotten. But namespace
A is not searched for the functions But this means that when a function
"func" is found in namespace B it will
only try this one and don''t bother with
the ones in namespace A ? This would
concur with what I''m finding in my
project, since the error does not occur
when I don''t define a function "func" in
namespace B (in my project that is
...).
This:

namespace A
{
void f();
}

namespace B
{
void g()
{
f(); // error
}
}

is invalid. Namespaces are meant to
prevent name clashing and therefore, it is
by design that function in a namespace are
not available from another, without special
syntax.
I did find the following solution: using A::func;
namespace B{
...

}
This is one way of doing it.
however this results in the error:
"ambiguous call to func ..."
Where? If it''s at namespace scope, this is
weird because in a namespace, local names
will always be preferred to other names.
Therefore, this is valid:

namespace A
{
void f(int) {}
}

using namespace A;
namespace B
{
void f(int) {}

void g()
{
f(10); // ok, calls B::f()
}
}

The problem is in the global scope:

int main()
{
using namespace A;
using namespace B;

f(10); // ambiguous
}
and explicit casting does not solve the
problem.
What casting?
Is there something I''m doing wrong ?




你不了解命名空间。阅读

关于他们。

Jonathan



You don''t understand namespaces. Read
about them.
Jonathan


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

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