<bdo id='o3PdU'></bdo><ul id='o3PdU'></ul>

<small id='o3PdU'></small><noframes id='o3PdU'>

        <tfoot id='o3PdU'></tfoot>

      1. <i id='o3PdU'><tr id='o3PdU'><dt id='o3PdU'><q id='o3PdU'><span id='o3PdU'><b id='o3PdU'><form id='o3PdU'><ins id='o3PdU'></ins><ul id='o3PdU'></ul><sub id='o3PdU'></sub></form><legend id='o3PdU'></legend><bdo id='o3PdU'><pre id='o3PdU'><center id='o3PdU'></center></pre></bdo></b><th id='o3PdU'></th></span></q></dt></tr></i><div id='o3PdU'><tfoot id='o3PdU'></tfoot><dl id='o3PdU'><fieldset id='o3PdU'></fieldset></dl></div>
        <legend id='o3PdU'><style id='o3PdU'><dir id='o3PdU'><q id='o3PdU'></q></dir></style></legend>
      2. “未定义的引用"到从静态方法访问的模板类的静态成员

        quot;undefined referencequot; to static member of template class accessed from static method(“未定义的引用到从静态方法访问的模板类的静态成员)
            <tbody id='cTINP'></tbody>
        • <legend id='cTINP'><style id='cTINP'><dir id='cTINP'><q id='cTINP'></q></dir></style></legend>
          <tfoot id='cTINP'></tfoot>

            <small id='cTINP'></small><noframes id='cTINP'>

              <bdo id='cTINP'></bdo><ul id='cTINP'></ul>

                • <i id='cTINP'><tr id='cTINP'><dt id='cTINP'><q id='cTINP'><span id='cTINP'><b id='cTINP'><form id='cTINP'><ins id='cTINP'></ins><ul id='cTINP'></ul><sub id='cTINP'></sub></form><legend id='cTINP'></legend><bdo id='cTINP'><pre id='cTINP'><center id='cTINP'></center></pre></bdo></b><th id='cTINP'></th></span></q></dt></tr></i><div id='cTINP'><tfoot id='cTINP'></tfoot><dl id='cTINP'><fieldset id='cTINP'></fieldset></dl></div>

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

                  问题描述

                  我有一个静态类方法,需要访问指针 MyTypePointer,因此必须将其声明为静态.由于它是一个模板类,我必须将方法放在头文件中,但我无法在头文件中定义 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.

                  因此我收到 "undefined Reference" 错误,因为 MyTypePointer 未声明.我怎样才能使这项工作/声明 MyTypePointer.

                  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
                  }
                  

                  非常感谢.

                  推荐答案

                  这是一个迟到的完整参考答案,因为这个问题被链接为另一个问题的参考.

                  It is a late answer for full reference, as this question is linked as a reference to another question.

                  静态字段声明但未定义的最小损坏示例可能是:

                  A minimal broken example of a static field declared but not defined could be :

                  template<typename T>
                  class A
                  {
                  public:
                      static T val;
                      static void init()
                      {
                          val=0;
                      }
                  };
                  
                  int main()
                  {
                      // A::init();
                      A<double>::init();
                      return 0;
                  }
                  

                  修复只是在类定义之后定义静态字段:

                  The fix is just to define the static field after the class definition :

                  template<typename T>
                  class A
                  {
                  public:
                      static T val;
                      static void init()
                      {
                          val=0;
                      }
                  };
                  
                  template<typename T> T A<T>::val; // only change here
                  
                  int main()
                  {
                      // A::init();
                      A<double>::init();
                      return 0;
                  }
                  

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                  <tfoot id='NQRwv'></tfoot>
                    <legend id='NQRwv'><style id='NQRwv'><dir id='NQRwv'><q id='NQRwv'></q></dir></style></legend>
                    • <bdo id='NQRwv'></bdo><ul id='NQRwv'></ul>
                        <tbody id='NQRwv'></tbody>

                        1. <i id='NQRwv'><tr id='NQRwv'><dt id='NQRwv'><q id='NQRwv'><span id='NQRwv'><b id='NQRwv'><form id='NQRwv'><ins id='NQRwv'></ins><ul id='NQRwv'></ul><sub id='NQRwv'></sub></form><legend id='NQRwv'></legend><bdo id='NQRwv'><pre id='NQRwv'><center id='NQRwv'></center></pre></bdo></b><th id='NQRwv'></th></span></q></dt></tr></i><div id='NQRwv'><tfoot id='NQRwv'></tfoot><dl id='NQRwv'><fieldset id='NQRwv'></fieldset></dl></div>

                          <small id='NQRwv'></small><noframes id='NQRwv'>