模板参数错误,C ++

时间:2011-01-30 17:55:45

标签: c++ templates arguments

我使用相当复杂的数据结构:

第1课:

template <class T>
class Number
{
private:
            T first;
            T second;

};

第2课:

template <typename Item>
struct TList
{
    typedef std::vector <Item> Type;
};

template <typename Item>
class GList
{
private:
            typename TList <Item>::Type items;

public:
            GList() : items (0) {}


public:
            Item & operator [] (int index ) {return items[index];}
};

第3课:

template <typename T, template <typename> class GList>
class sortIndices 
{

private:
    const GList <T> *l;

public:
    sortIndices ( const GList <T> *l_ ) : l ( l_ ) {}

    bool operator() ( const unsigned int &i_p1, const unsigned int &i_p2 ) const
    {
        return true;
    }

};

第4课:

template <typename T, template <typename> class List>
struct TISet
{
    typedef std::set <unsigned int, sortIndices <T, List> >  Type;
};


template <typename T, template <typename> class List>
struct TSample
{
    T res;
    typename TISet <T, List> ::Type indices;
    TSample ( const List <T> *nl ) : res(0), indices ( nl ) {}
};

第5课:

template <typename T, template <typename> class List>
struct TResults
{
    typedef std::set < TSample <T, List>, sortIndices <T, List> > Type;
};

第6课:

class Test
{
public:
    template <typename T, template <typename> class List>
    static void function (List <T> *l, typename TResults <T, List> ::Type *result) {}
};

使用以下对象作为静态函数的参数的问题......

TResults <double, GList >::Type t_results; 

我的第一个问题:如何正确传递结构地址?

主程序:

int main() 
{
    GList <Number <double> *> plist; //Create list of numbers
    TResults <double, GList >::Type t_results; //Create empty results

    Test::function(&plist, &t_results); //ERROR, test function
}

错误代码:

Error   1 error C2664: 'void Test::function< Number <T> *, GList>( GList <Item> *,std::set<_Kty, _Pr> *)' :
cannot convert parameter 2 from 'std::set<_Kty,_Pr> *' to 'std::set<_Kty,_Pr> *'

with
      [
          T=double,
          Item=Number<double> *,
          _Kty=TSample<Number<double> *,GList>,
          _Pr=sortIndices<Number<double> *,GList>
      ]
      and
      [
          _Kty=TSample<double,GList>,
          _Pr=sortIndices<double,GList>
      ]
      and
      [
          _Kty=TSample<Number<double> *,GList>,
          _Pr=sortIndices<Number<double> *,GList>
      ]

修改后

class Test
{
public:
    template <typename T, template <typename> class List>
    static void function (typename TResults <T, List> ::Type *result) {}
};

int main() 
{
    GList <Number <double> *> plist; //Create list of numbers
    TResults <double, GList >::Type t_results; //Create empty results

    Test::function(&t_results); //ERROR, test function
}

出现以下错误:

错误1错误C2783:'void Test :: function(TResults :: Type *)':无法推断'T'的模板参数

感谢您的时间和帮助...

2 个答案:

答案 0 :(得分:1)

没有匹配的专业化。你可以打电话给

Test::function( GList<Number<double>*>*, TResults<Number<double>*, GList>::Type* );

Test::function( GList<double>*, TResults<double, GList>::Type* );

但你所拥有的论据类型并没有结合在一起。

答案 1 :(得分:1)

检查http://www.ideone.com/tTETR

我更改了类型并将比较器参数添加到TResults::Type ctor。