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()'不帶任何參數,但是您的派生類函數'foo()'將'string'作爲參數。 –
@克勞斯我想這不是那個笨蛋...... –
@EdgarRokyan:葉普,你說得對。刪除了評論和投票...謝謝 – Klaus