c++ - error : expected constructor, 析构函数,或者静态模板中 ‘'之前的类型转换

标签 c++ templates static

<分区>

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

我的错误是

     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_;

你能帮帮我吗?

最佳答案

T::Params 之前需要 typename

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_;
^^^^^^^^^^^^^^^^^^^^^^^

关于c++ - error : expected constructor, 析构函数,或者静态模板中 ‘'之前的类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19888524/

相关文章:

c++ - 这种类型的双关语定义明确吗?

c++ - gcc 中的模板显式实例化(定义和声明)

c++ - friend &&模板

iphone - 如何在Xcode 4中创建项目模板

java - 为什么我不能在类的构造函数中调用静态方法?

c++ - 静态数组的行为改变了吗?

c++ fstream不打印像ρ和Δ这样的符号来正确归档

c++ - boost::lexical_cast<signed char> 无法处理负数?

java - 为什么我们不应该在 java 中使用 protected static

c++ - 打开元组可变参数模板参数的包装