由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - An interesting C++ compile error
相关主题
A C++ compiler related interview questionC++ class library
c++ dynamic cast问个虚函数的作用
C++小插曲问个virtual table 的问题
question for C++ constantprivate destructor
template 类的继承问题一个 C++ STL base type 的问题
C++ online Test 2题A tech question (转载)
C array谁来解释一下这个是compiler问题吗?
请问c++为什么会编译失败?关于Makefile的一个问题
相关话题的讨论汇总
话题: foo话题: derived话题: int话题: base话题: compile
进入Programming版参与讨论
1 (共1页)
T***B
发帖数: 137
1
When I compile the following code with g++, I got following error:
g++ tt.cpp
tt.cpp: In static member function `static void Derived::test()':
tt.cpp:36: no matching function for call to `Derived::foo(int)'
tt.cpp:23: candidates are: virtual int Derived::foo(int, int)
Seems the compiler doesn't try to look for the foo with 2 paras in the base
class if there is one foo method available in derived class. Any reason
compiler chooses to do this this way. How to make this thing work (elegantly
)?
Tha
h****e
发帖数: 2125
2
the compiler expects you to override virtual foo(int) method as well,
otherwise it's confused. just simply do in Derived:
"
virtual int foo(int i)
{
return Base::foo(i);
}
"

base
elegantly

【在 T***B 的大作中提到】
: When I compile the following code with g++, I got following error:
: g++ tt.cpp
: tt.cpp: In static member function `static void Derived::test()':
: tt.cpp:36: no matching function for call to `Derived::foo(int)'
: tt.cpp:23: candidates are: virtual int Derived::foo(int, int)
: Seems the compiler doesn't try to look for the foo with 2 paras in the base
: class if there is one foo method available in derived class. Any reason
: compiler chooses to do this this way. How to make this thing work (elegantly
: )?
: Tha

o*o
发帖数: 404
3
TIC C. 14 Name Hiding.

base
elegantly

【在 T***B 的大作中提到】
: When I compile the following code with g++, I got following error:
: g++ tt.cpp
: tt.cpp: In static member function `static void Derived::test()':
: tt.cpp:36: no matching function for call to `Derived::foo(int)'
: tt.cpp:23: candidates are: virtual int Derived::foo(int, int)
: Seems the compiler doesn't try to look for the foo with 2 paras in the base
: class if there is one foo method available in derived class. Any reason
: compiler chooses to do this this way. How to make this thing work (elegantly
: )?
: Tha

T***B
发帖数: 137
4
Thanks 坚强. Your solution works. But I may use following approach instead
to make it cleaner.
virtual int foo(int i, int j = 0);
t****t
发帖数: 6806
5
why not write
using Base::foo;
in Derived?

【在 h****e 的大作中提到】
: the compiler expects you to override virtual foo(int) method as well,
: otherwise it's confused. just simply do in Derived:
: "
: virtual int foo(int i)
: {
: return Base::foo(i);
: }
: "
:
: base

h****e
发帖数: 2125
6
i've no problem with that.

【在 t****t 的大作中提到】
: why not write
: using Base::foo;
: in Derived?

T***B
发帖数: 137
7
I feel it's not natural for the method of the derived class to do nothing
but calling the Base::foo.
virtual int foo(int i)
{
return Base::foo(i);
}
1 (共1页)
进入Programming版参与讨论
相关主题
关于Makefile的一个问题template 类的继承问题
关于C++ STL编译的疑问C++ online Test 2题
a question about CASTC array
c++标准函数传递一问 请问c++为什么会编译失败?
A C++ compiler related interview questionC++ class library
c++ dynamic cast问个虚函数的作用
C++小插曲问个virtual table 的问题
question for C++ constantprivate destructor
相关话题的讨论汇总
话题: foo话题: derived话题: int话题: base话题: compile