“未定义参考”到静态字段模板专业化 [英] "undefined reference" to static field template specialization

查看:126
本文介绍了“未定义参考”到静态字段模板专业化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板类的头文件,它只有静态函数和字段。

I have and header with a template class, which has only static functions and fields.

template<typename T> class Luaproxy {

    static std::map<std::string, fieldproxy> fields;
    static const char * const CLASS_NAME;
    static void addfields();
    static int __newindex(lua_State * l){
        //implemented stuff, references to fields...
    }
    //etc
}

正如你所看到的,一些函数只能被声明,因为我打算用模板专门化来实现它们。

As you can see some of the functions are only declared, because I intend to implement them with template specialization.

在.ccp文件中,我有:

In a .ccp file I have:

struct test { int a; }
template<> map<string, fieldproxy> Luaproxy<test>::fields;
template<> const char * const Luaproxy<test>::CLASS_NAME=typeid(test).name();
template<> void Luaproxy<test>::addfields(){
    //stuff, references to fields...
}

从标题中实现的两个函数中获取未定义的引用错误到 Luaproxy< test> :: fields 在.cpp中。请注意,在链接中找到 Luaproxy< test> :: CLASS_NAME Luaproxy< test> :: addfields

I get undefined reference errors to Luaproxy<test>::fields from both functions that are implemented in the header and those that are only specialized in the .cpp. Note that Luaproxy<test>::CLASS_NAME and Luaproxy<test>::addfields seem to be found in linking.

是什么使得映射如此特别?

推荐答案

我终于得到它的工作,但我不能真正说为什么我的编译器(gcc 4.6.1)需要这样: template< std :: map< std :: string,fieldproxy> Luaproxy< test> :: fields = std :: map< std :: string,fieldproxy>();

I finally managed to get it working, but I couldn't really say why my compiler (gcc 4.6.1) needs it this way: template<> std::map<std::string, fieldproxy> Luaproxy<test>::fields=std::map<std::string, fieldproxy>();

显式构造函数似乎说服gcc有效地发射变量。我要求#gcc澄清,但不幸的是,频道总是保持沉默。

The explicit constructor seem to convince gcc to effectively emit the variable. I asked #gcc for clarification, but unfortunately that channel is always silent.

这篇关于“未定义参考”到静态字段模板专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆