0
は、次の例を考えてみてください過負荷に基底クラスの型パラメータ法を見つけることができません。C++テンプレート:
#include <iostream>
class Base {
public:
virtual void foo(std::string str) = 0;
void foo() { foo("LOL"); }
};
class Derived : public Base {
public:
void foo(std::string str) { std::cout << str << std::endl; }
};
template<class T> class MyTemplate {
public:
void print() { a.foo(); }
T a;
};
int
main(int argc, char** argv)
{
MyTemplate<Derived> a;
a.print();
}
コンパイルすると、私は次のようなエラーがあります:それは解決策があることがわかっ
main.cpp: In instantiation of ‘void MyTemplate<T>::print() [with T = Derived]’:
main.cpp:24:11: required from here
main.cpp:16:18: error: no matching function for call to ‘Derived::foo()’
void print() { a.foo(); }
^
main.cpp:16:18: note: candidate is:
main.cpp:11:8: note: virtual void Derived::foo(std::string)
void foo(std::string str) { std::cout << str << std::endl; }
^
main.cpp:11:8: note: candidate expects 1 argument, 0 provided
を書く:
void print() { a.Base::foo(); }
なぜですか? G ++がそれ自身のBase :: foo()メソッドを見つけられないのはなぜですか?
おかげ
'a.foo()'は引数をとりませんが、 'Derived clas '関数' foo() 'は' string'を引数として取ります。 –
@Klaus私はそういうわけではないと思います... –
@EdgarRokyan:Yepp、あなたは正しいです。コメントと投票を削除しました...ありがとう – Klaus