0

我正在尝试定义一个可以通过字符串搜索的函数指针表。我定义了以下模板类,但静态成员有问题。

我的错误是

     test.cpp:18: error: expected constructor, destructor, or type conversion 
     before ‘Attribute’

即使添加了一个构造函数,它也不起作用:你知道编译器告诉我的原因是什么,以及这段简单的代码中缺少什么吗?

     #include<iostream>
     #include<map>
     #include<boost/function.hpp>

     using namespace std;
     using namespace boost;

     template<class T>
     class Attribute {
     public:             
       typedef map<string, boost::function<void(T*,string) > > Params;
       static Params params_;
     };

     template<class T>
     T::Params Attribute<T>::params_;

你能帮助我吗?

4

1 回答 1

3

你需要typename之前T::Params.

template<class T>
class Attribute
{
public:
    typedef map<string, boost::function<void(T*,string) > > Params;
    static typename T::Params params_;
           ^^^^^^^^^^^^
};

template<class T>
typename T::Params Attribute<T>::params_;
^^^^^^^^

或者...

template<class T>
class Attribute
{
public:
    typedef map<string,int > Params;
    static Params params_;
};

template<class T>
typename Attribute<T>::Params Attribute<T>::params_;
^^^^^^^^^^^^^^^^^^^^^^^
于 2013-11-10T10:19:32.453 回答