gcc:模板参数的数量错误

gcc: wrong number of template arguments

本文关键字:错误 参数 gcc      更新时间:2023-10-16

为什么这段代码在VS13中编译成功,而gcc编译失败?

/////    file my_map.h /////
namespace my 
{
    // my custom map
    template<typename K, typename V, typename order = less<K>, typename allocator = cached_alloc<page_allocator<pair<K,V> > > >
    class map : public set_base<pair<K, V>, K, select1st, order, ins_unique, allocator>
    {
        ...
    };
}
/////    file test.h /////
#include "my_map.h"
template <typename T>    
class Base
{
protected:
    typedef my::map<T, double> MyMap;
    MyMap m_map;                                // this is line NN
public:
    void func(const T& key)
    {
        typename MyMap::iterator it = m_map.find(key);
        if(it != m_map.end()) {
            // ....
        }
    }
};
class Inherited1 : public Base <char>
{ };
class Inherited2 : public Base <int>
{ };

它会导致以下错误(gcc 4.1.2)

filepath.h:LineNN error: wrong number of template arguments (1, should be 4)
..: error: provided for 'template<class K, class V, class order, class allocator> class my::map'

我还不清楚编译器所说的"错误数量的模板参数"到底是什么意思?

您使用的编译器太旧了。Gcc 4.1.2于7年前发布。它和那个时代的旧VC编译器一样有漏洞。由于新的编译器运行良好,很难找到问题所在。尝试更新编译器。