Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板的私有静态变量成员的未定义符号_C++_Templates_Undefined_Instantiation_Static Members - Fatal编程技术网

C++ 模板的私有静态变量成员的未定义符号

C++ 模板的私有静态变量成员的未定义符号,c++,templates,undefined,instantiation,static-members,C++,Templates,Undefined,Instantiation,Static Members,我有一个奇怪的问题。我想实现类似于()的方法来检索C++对象的名称。但是我的对象使用模板。这是我的修改: template <int dim> class AspenFunction: public Function <dim> { // define the type of the map to handle all functions typedef std::map<std::string, AspenFunction<dim>*(*)()

我有一个奇怪的问题。我想实现类似于()的方法来检索C++对象的名称。但是我的对象使用模板。这是我的修改:

template <int dim>
class AspenFunction: public Function <dim>
{
  // define the type of the map to handle all functions
  typedef std::map<std::string, AspenFunction<dim>*(*)()> map_type;

  static AspenFunction * create_aspen_function (const std::string& s)
  {
    typename map_type::iterator it = getMap()->find(s);
    AssertThrow(it == getMap()->end(), ExcMessage("The aspen_function name '" + s + "' is not registered."));
    return it->second();
  }

protected:
  static map_type * getMap()
  {
    if (!_map)
      _map = new map_type;
    return _map;
  }

private:
  static map_type * _map;
};


template <typename T, int dim>
AspenFunction<dim> * createT() { return new T; }

template <typename T, int dim>
class Registrar: AspenFunction <dim>
{
public:
  Registrar(const std::string& s)
  {
    AspenFunction<dim>::getMap()->insert(std::make_pair(s, &createT<T, dim>));
  }
};

template class AspenFunction<2>;
但我得到了以下错误:

/include/equation_handler.h:94:62: error: explicit specialization of '_map' after instantiation
  template <> AspenFunction<2>::map_type * AspenFunction<2>::_map;
                                                             ^
/include/equation_handler.h:69:14: note: implicit instantiation first required here
        if (!_map)
             ^

您只声明了
静态
数据成员
\u映射
,它缺少定义。在类定义之后立即添加定义(最好)

template <int dim>
typename AspenFunction<dim>::map_type *AspenFunction<dim>::_map;

第一次调用
getMap()
时将创建
instance
对象,C++11保证实例化是线程安全的。

Do
template AspenFunction::map\u type*AspenFunction::\u map错过这个我觉得很愚蠢!有了所有的模板关键字和继承,我错过了通常的声明和定义。
/include/equation_handler.h:94:62: error: explicit specialization of '_map' after instantiation
  template <> AspenFunction<2>::map_type * AspenFunction<2>::_map;
                                                             ^
/include/equation_handler.h:69:14: note: implicit instantiation first required here
        if (!_map)
             ^
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix
template <int dim>
typename AspenFunction<dim>::map_type *AspenFunction<dim>::_map;
static map_type& getMap()
{
  static map_type instance;
  return instance;
}