我有一個模板化的基類,提供了一個方法remove()。我有一個從模板化基類派生的類,它不隱藏remove()方法。但是,基於模板的類的remove方法不可見。爲什麼?有沒有辦法解決這個問題(我的意思不是我最後想到的'技巧')?爲什麼模板化基類中的方法不可見?
我已經剝離下來到一個小的代碼示例:
#include <map>
#include <iostream>
#include <boost/shared_ptr.hpp>
// Common cache base class. All our caches use a map, expect children to
// specify their own add, remove and modify methods, but the base supplies a
// single commont remove too.
template <class T>
class cache_base {
public:
cache_base() {};
virtual ~cache_base() {};
virtual void add(uint32_t id) = 0;
virtual void remove(uint32_t id) = 0;
void remove() {
std::cout << "This is base remove\n";
};
virtual void modify(uint32_t id) = 0;
protected:
typedef std::map< uint32_t, typename T::SHARED_PTR_T> DB_MAP_T;
DB_MAP_T m_map;
};
// A dummy item to be managed by the cache.
class dummy {
public:
typedef boost::shared_ptr<dummy> SHARED_PTR_T;
dummy() {};
~dummy() {};
};
// A dummy cache
class dummy_cache :
public cache_base<dummy>
{
public:
dummy_cache() {};
virtual ~dummy_cache() {};
virtual void add(uint32_t id) {};
virtual void remove(uint32_t id) {};
virtual void modify(uint32_t id) {};
};
int
main()
{
dummy_cache D;
D.remove();
return(0);
}
這段代碼編譯失敗,給我下面的錯誤
g++ -g -c -MD -Wall -Werror -I /views/LU-7.0-DRHA-DYNAMIC/server/CommonLib/lib/../include/ typedef.cxx
typedef.cxx: In function 'int main()':
typedef.cxx:67: error: no matching function for call to 'dummy_cache::remove()'
typedef.cxx:54: note: candidates are: virtual void dummy_cache::remove(uint32_t)
make: *** [typedef.o] Error 1
我不知道它是否有所作爲,但我使用g ++版本4.1.2 20070115.
此外,我發現如果我將下面的刪除方法添加到dummy_cache
它的作品。但是,我不得不在dummy_cache中添加一個從屬方法來公開一個公有基方法。
void remove() {return cache_base<dummy>::remove(); }
的[C++重寫/過載問題(http://stackoverflow.com/questions/1484641/c-override-overload-problem) – 2012-04-18 19:37:25