继承问题 [英] Problem with inheritance
问题描述
有人可以告诉我为什么下面的代码不起作用:
Can someone tell me why the following code doesn''t work:
TestClass.cpp
-------------
A级
公开:
虚拟空读(wchar_t& ch){read(& ch,0,1); }
虚拟空读(wchar_t * buf,int off,int len)= 0;
};
B类:公共虚拟A
{
public:
virtual void read(wchar_t * buf,int off,int len){}
};
int main(int argc,char * argv [])
{
wchar_t ch;
B myclass;
myclass.read(ch);
}
我试过两个gcc 2.96和gcc 3.2。我得到:
TestClass.cpp:在函数`int main(int,char **)''中:
TestClass.cpp:21:没有用于调用`B ::的匹配函数read(wchar_t&)''
TestClass.cpp:14:候选者是:virtual void B :: read(wchar_t *,int,int)
TestClass.cpp
-------------
class A
{
public:
virtual void read(wchar_t& ch) { read(&ch, 0, 1); }
virtual void read(wchar_t* buf, int off, int len) = 0;
};
class B : public virtual A
{
public:
virtual void read(wchar_t* buf, int off, int len) {}
};
int main(int argc, char* argv[])
{
wchar_t ch;
B myclass;
myclass.read(ch);
}
I have tried both gcc 2.96 and gcc 3.2. I get:
TestClass.cpp: In function `int main(int, char**)'':
TestClass.cpp:21: no matching function for call to `B::read(wchar_t&)''
TestClass.cpp:14: candidates are: virtual void B::read(wchar_t*, int, int)
难道不应该从A继承读取(wchar_t& ch)吗?
Shouldn''t B have inherited read(wchar_t& ch) from A?
推荐答案
" ; Victor Chew < VC *** @ post1.com>在留言中写道
news:bg ********** @ mawar.singnet.com.sg ...
"Victor Chew" <vc***@post1.com> wrote in message
news:bg**********@mawar.singnet.com.sg...
有人可以告诉我为什么以下代码不起作用:
Can someone tell me why the following code doesn''t work:
> TestClass.cpp
> -------------
A级
{
公开:
虚拟空读(wchar_t& ch){read(& ch,0 ,1); }
虚拟空读(wchar_t * buf,int off,int len)= 0;
};
B类:公共虚拟A
{
public:
virtual void read(wchar_t * buf,int off,int len){}
};
int main(int argc,char * argv [])
{
wchar_t ch;
B myclass;
myclass.read(ch);
}
我已经尝试过gcc 2.96和gcc 3.2。我得到:
> TestClass.cpp
> -------------
class A
{
public:
virtual void read(wchar_t& ch) { read(&ch, 0, 1); }
virtual void read(wchar_t* buf, int off, int len) = 0;
};
class B : public virtual A
{
public:
virtual void read(wchar_t* buf, int off, int len) {}
};
int main(int argc, char* argv[])
{
wchar_t ch;
B myclass;
myclass.read(ch);
}
I have tried both gcc 2.96 and gcc 3.2. I get:
TestClass.cpp:在函数`int main(int,char **)''中:
TestClass.cpp:21:没有用于调用的匹配函数`B :: read(wchar_t&)''
TestClass.cpp:14:候选者是:virtual void B :: read(wchar_t *,int,
TestClass.cpp: In function `int main(int, char**)'':
TestClass.cpp:21: no matching function for call to `B::read(wchar_t&)''
TestClass.cpp:14: candidates are: virtual void B::read(wchar_t*, int,
int)
不应该从A继承read(wchar_t& ch)吗?
int)
Shouldn''t B have inherited read(wchar_t& ch) from A?
你的基类函数virtual void read(wchar_t& ch)已被隐藏的
派生类
函数虚拟空读(wchar_t * buf,int off,int len)。
Your base class function virtual void read(wchar_t& ch) has been hidden by
the derived class
function virtual void read(wchar_t* buf, int off, int len).
< br>
" Victor Chew" < VC *** @ post1.com>在留言中写道
news:bg ********** @ mawar.singnet.com.sg ...
"Victor Chew" <vc***@post1.com> wrote in message
news:bg**********@mawar.singnet.com.sg...
有人可以告诉我为什么以下代码不起作用:
Can someone tell me why the following code doesn''t work:
> TestClass.cpp
> -------------
A级
{
公开:
虚拟空读(wchar_t& ch){read(& ch,0 ,1); }
虚拟空读(wchar_t * buf,int off,int len)= 0;
};
B类:公共虚拟A
{
public:
virtual void read(wchar_t * buf,int off,int len){}
};
int main(int argc,char * argv [])
{
wchar_t ch;
B myclass;
myclass.read(ch);
}
> TestClass.cpp
> -------------
class A
{
public:
virtual void read(wchar_t& ch) { read(&ch, 0, 1); }
virtual void read(wchar_t* buf, int off, int len) = 0;
};
class B : public virtual A
{
public:
virtual void read(wchar_t* buf, int off, int len) {}
};
int main(int argc, char* argv[])
{
wchar_t ch;
B myclass;
myclass.read(ch);
}
我试过两个gcc 2.96和gcc 3.2。我得到:
I have tried both gcc 2.96 and gcc 3.2. I get:
TestClass.cpp:在函数`int main(int,char **)''中:
TestClass.cpp:21:没有用于调用的匹配函数`B :: read(wchar_t&)''
TestClass.cpp:14:候选者是:virtual void B :: read(wchar_t *,int,int)
TestClass.cpp: In function `int main(int, char**)'':
TestClass.cpp:21: no matching function for call to `B::read(wchar_t&)''
TestClass.cpp:14: candidates are: virtual void B::read(wchar_t*, int, int)
难道不应该从A继承read(wchar_t& ch)吗?
Shouldn''t B have inherited read(wchar_t& ch) from A?
B :: read(wchar_t * buf,int off,int len)隐藏A: :read(wchar_t& ch)。
如果覆盖重载的基类函数,
重新定义完整的函数集。
-
ES Kim
B::read(wchar_t* buf, int off, int len) hides A::read(wchar_t& ch).
If you override an overloaded base class functions,
redefine full set of the functions.
--
ES Kim
2003年7月31日星期四11:42:17 + 0800,Victor Chew< vc ** *@post1.com>写道:
On Thu, 31 Jul 2003 11:42:17 +0800, Victor Chew <vc***@post1.com> wrote:
有人能告诉我为什么下面的代码不起作用:
Can someone tell me why the following code doesn''t work:
TestClass.cpp
--- ----------
A级
{
公开:
虚拟空读(wchar_t& ch){read(& ch,0,1) ; }
虚拟空读(wchar_t * buf,int off,int len)= 0;
};
B类:公共虚拟A
{
public:
使用A :: read;
虚拟空读(wchar_t * buf,int off,int len){}
} ;
int main(int argc,char * argv [])
{
wchar_t ch;
B myclass;
myclass.read(ch) ;
}
TestClass.cpp
-------------
class A
{
public:
virtual void read(wchar_t& ch) { read(&ch, 0, 1); }
virtual void read(wchar_t* buf, int off, int len) = 0;
};
class B : public virtual A
{
public:
using A::read;
virtual void read(wchar_t* buf, int off, int len) {}
};
int main(int argc, char* argv[])
{
wchar_t ch;
B myclass;
myclass.read(ch);
}
我试过gcc 2.96和gcc 3.2。我得到:
I have tried both gcc 2.96 and gcc 3.2. I get:
TestClass.cpp:在函数`int main(int,char **)''中:
TestClass.cpp:21:没有用于调用的匹配函数`B :: read(wchar_t&)''
TestClass.cpp:14:候选者是:virtual void B :: read(wchar_t *,int,int)
TestClass.cpp: In function `int main(int, char**)'':
TestClass.cpp:21: no matching function for call to `B::read(wchar_t&)''
TestClass.cpp:14: candidates are: virtual void B::read(wchar_t*, int, int)
不应该从A继承读取(wchar_t& ch)吗?
Shouldn''t B have inherited read(wchar_t& ch) from A?
它有,但没有''使用''它被隐藏的其他阅读。
现在不要问我_为什么有人认为这是明智的。
It has, but without the ''using'' it''s hidden by the other read.
Now don''t ask me _why_ someone thought that would be sensible.
这篇关于继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!