这应该是编译错误吗? [英] Should this really be a compile error?

查看:77
本文介绍了这应该是编译错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们遇到了这个问题,我不明白为什么编译器(Sun One

Studio 8和g ++ 2.95)认为这是一个错误:

-------------------


A级

{

public:

void foo();


};


B级:公开A

{

public:

void foo(int i);


};


void B :: foo(int i)

{

foo(); //这两个编译器都给出了错误。


}


class C

{


public:

void foo();


void foo(int i);


};


void C :: foo(int i)

{

foo(); //两个编译器都没有给出错误。


}


---------------- -----


显然它与继承有关,但在B'范围内有一个

有效的foo()调用是foo(int)。如果我们称之为A :: foo()

,它可以毫无怨言地工作。

We ran into this problem and I can''t see why the compilers (Sun One
Studio 8 and g++ 2.95) think it''s an error:

-------------------

class A
{
public:
void foo();

};

class B : public A
{
public:
void foo(int i);

};

void B::foo(int i)
{
foo(); // Both compilers give an error here.

}

class C
{

public:
void foo();

void foo(int i);

};

void C::foo(int i)
{
foo(); // Neither compiler gives an error here.

}

---------------------

Clearly it has something to do with the inheritance, but there is a
valid foo() call in the scope of B''s foo(int). If we call "A::foo()"
instead, it works without complaint.

推荐答案

ro*********@gmail.com 写道:
ro*********@gmail.com wrote:

我们遇到了这个问题,我不明白为什么编译器(Sun One

Studio 8和g ++ 2.95)认为它是一个错误:
We ran into this problem and I can''t see why the compilers (Sun One
Studio 8 and g++ 2.95) think it''s an error:



关于Sun编译器的Dunno,bug g ++ 2.95已经很老了。你应该升级

吧。

Dunno about the Sun compiler, bug g++ 2.95 is very old. You should upgrade
it.


class A

{

public:

void foo();


};


B级:公众A

{

public:

void foo(int i);


};


void B :: foo(int i)

{

foo(); //这两个编译器都给出了错误。
class A
{
public:
void foo();

};

class B : public A
{
public:
void foo(int i);

};

void B::foo(int i)
{
foo(); // Both compilers give an error here.



他们是coorect。

They are coorect.


}


class C $ / $
{


public:

void foo();


void foo(int i);


};


void C :: foo(int i)

{

foo(); //这两个编译器都没有给出错误。
}

class C
{

public:
void foo();

void foo(int i);

};

void C::foo(int i)
{
foo(); // Neither compiler gives an error here.



他们再次正确。

They are again correct.


}


---------------------


显然它与继承有关,但有一个

在B'的foo(int)范围内调用有效的foo()。如果我们称之为A :: foo()

,则无需投诉。
}

---------------------

Clearly it has something to do with the inheritance, but there is a
valid foo() call in the scope of B''s foo(int). If we call "A::foo()"
instead, it works without complaint.



这就是应该如何。派生类中的函数隐藏了具有相同名称的基类的所有

函数。您必须使用A :: foo()或者使用A :: foo添加:


;


到B'的类定义。

That''s how it''s supposed to be. A function in a derived class hides all
functions of the base class that have the same name. You must either
qualify it with A::foo() or add:

using A::foo;

to B''s class definition.


在文章< 11 ******************* ***@m73g2000cwd.googlegroups .com> ;,
ro ********* @ gmail .com 写道:
In article <11**********************@m73g2000cwd.googlegroups .com>,
ro*********@gmail.com wrote:

我们遇到了这个问题,我不明白为什么编译器(Sun One

Studio 8和g ++ 2.95)认为这是一个错误:


-------------------


A级
{

公开:

void foo();


};


B级:公共A

{

公开:

无效foo(int i);


};


void B :: foo(int i)

{

foo(); //两个编译器都在这里给出错误。


}
We ran into this problem and I can''t see why the compilers (Sun One
Studio 8 and g++ 2.95) think it''s an error:

-------------------

class A
{
public:
void foo();

};

class B : public A
{
public:
void foo(int i);

};

void B::foo(int i)
{
foo(); // Both compilers give an error here.

}



''foo()''隐藏在这个范围内因此错误。


B级:公共A {

使用foo;

公开:

void foo(int i);

};


将停止错误。

''foo()'' is hidden in this scope, thus the error.

class B : public A {
using foo;
public:
void foo(int i);
};

Will stop the error.


罗尔夫马格纳斯 < ra ****** @ t-online.dewrote in message

news:e8 ************* @ news.t-online.com。 ..
"Rolf Magnus" <ra******@t-online.dewrote in message
news:e8*************@news.t-online.com...
ro ********* @ gmail。 com 写道:
ro*********@gmail.com wrote:

>我们遇到了这个问题,我不明白为什么编译器(Sun One
Studio 8和g ++ 2.95)认为这是一个错误:
>We ran into this problem and I can''t see why the compilers (Sun One
Studio 8 and g++ 2.95) think it''s an error:



关于Sun编译器的Dunno,bug g ++ 2.95已经很老了。你应该升级

吧。


Dunno about the Sun compiler, bug g++ 2.95 is very old. You should upgrade
it.


> A级
{
公开:
void foo ();

};

B班:公众A
公开:
void foo(int i);

};

void B :: foo(int i)
{
foo(); //这两个编译器都给出了错误。
>class A
{
public:
void foo();

};

class B : public A
{
public:
void foo(int i);

};

void B::foo(int i)
{
foo(); // Both compilers give an error here.



他们是coorect。


They are coorect.


>}

C类<公开:
void foo();

void foo(int i);

};

void C :: foo(int i)
{
foo(); //这两个编译器都没有给出错误。
>}

class C
{

public:
void foo();

void foo(int i);

};

void C::foo(int i)
{
foo(); // Neither compiler gives an error here.



他们再次正确。


They are again correct.


>}

- -------------------

显然它与继承有关,但有一个
有效的foo()调用在B'的foo(int)范围内。如果我们称之为A :: foo()
,它就可以毫无怨言地运作。
>}

---------------------

Clearly it has something to do with the inheritance, but there is a
valid foo() call in the scope of B''s foo(int). If we call "A::foo()"
instead, it works without complaint.



这就是应该如何。派生类中的函数隐藏了具有相同名称的基类的所有

函数。您必须使用A :: foo()或者使用A :: foo添加:


;


到B'的类定义。


That''s how it''s supposed to be. A function in a derived class hides all
functions of the base class that have the same name. You must either
qualify it with A::foo() or add:

using A::foo;

to B''s class definition.



为什么它应该是这样的,出于兴趣?有什么问题

/不能隐藏同名基本功能?


干杯,

Stu

Why is it supposed to be like that, out of interest? What''s the problem
with/disadvantage of not hiding base functions with the same name?

Cheers,
Stu


这篇关于这应该是编译错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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