“未定义参考”到从静态方法访问的模板类的静态成员 [英] "undefined reference" to static member of template class accessed from static method

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

问题描述

我有一个静态类方法,需要访问一个指针MyTypePointer,因此必须声明为静态。因为它是一个模板类,我必须将方法放在头文件中,但我不能在头中定义MyTypePointer。



所以我得到未定义的引用错误,因为MyTypePointer未声明。我如何使这工作/声明MyTypePointer。

  myclass.h 

template< typename A,typename B>
class PathfindingClass {
typedef std :: vector< GenericEdgeClass< A,B> *> MyType;
static MyType * MyTypePointer;
};

template< typename A,B>
void MyClass< A,B> :: MyMethod(int x,int y){
//使用MyTypePointer执行某些操作
}
pre>

非常感谢。

解决方案

static MyType * MyTypePointer; 声明一个对象。您仍然必须在模板类定义之外定义

  template< class A, B类> 
typename PathfindingClass< A,B> :: MyType *
PathfindingClass< A,B> :: MyTypePointer;


I have a static class method that needs access to a pointer MyTypePointer that therefore has to be declared static. Since it is a template class I have to put the methods inside the header file but I can't define MyTypePointer in the header.

So I get the "undefined Reference" error since MyTypePointer is not declared. How can I make this work / declare MyTypePointer.

myclass.h

template <typename A, typename B>
class PathfindingClass {
    typedef std::vector<GenericEdgeClass<A, B>*> MyType;
    static MyType *MyTypePointer;
};

template <typename A, B>
void MyClass<A, B>::MyMethod(int x, int y) { 
    //do something with MyTypePointer
}

Thank you very much.

解决方案

In the template definition, static MyType *MyTypePointer; declares an object. You still have to define it, outside the template class definition:

template <class A, class B>
typename PathfindingClass<A, B>::MyType*
PathfindingClass<A, B>::MyTypePointer;

这篇关于“未定义参考”到从静态方法访问的模板类的静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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