1. <small id='BGBFs'></small><noframes id='BGBFs'>

    <legend id='BGBFs'><style id='BGBFs'><dir id='BGBFs'><q id='BGBFs'></q></dir></style></legend>

        <bdo id='BGBFs'></bdo><ul id='BGBFs'></ul>
    1. <tfoot id='BGBFs'></tfoot>
      <i id='BGBFs'><tr id='BGBFs'><dt id='BGBFs'><q id='BGBFs'><span id='BGBFs'><b id='BGBFs'><form id='BGBFs'><ins id='BGBFs'></ins><ul id='BGBFs'></ul><sub id='BGBFs'></sub></form><legend id='BGBFs'></legend><bdo id='BGBFs'><pre id='BGBFs'><center id='BGBFs'></center></pre></bdo></b><th id='BGBFs'></th></span></q></dt></tr></i><div id='BGBFs'><tfoot id='BGBFs'></tfoot><dl id='BGBFs'><fieldset id='BGBFs'></fieldset></dl></div>
    2. 静态成员中对类静态成员的未定义引用

      时间:2023-12-02
          <tbody id='62mhN'></tbody>

            <tfoot id='62mhN'></tfoot>
              • <bdo id='62mhN'></bdo><ul id='62mhN'></ul>

                <small id='62mhN'></small><noframes id='62mhN'>

                <legend id='62mhN'><style id='62mhN'><dir id='62mhN'><q id='62mhN'></q></dir></style></legend>

                <i id='62mhN'><tr id='62mhN'><dt id='62mhN'><q id='62mhN'><span id='62mhN'><b id='62mhN'><form id='62mhN'><ins id='62mhN'></ins><ul id='62mhN'></ul><sub id='62mhN'></sub></form><legend id='62mhN'></legend><bdo id='62mhN'><pre id='62mhN'><center id='62mhN'></center></pre></bdo></b><th id='62mhN'></th></span></q></dt></tr></i><div id='62mhN'><tfoot id='62mhN'></tfoot><dl id='62mhN'><fieldset id='62mhN'></fieldset></dl></div>
                本文介绍了静态成员中对类静态成员的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在 C++ 中创建了一个带有自引用类的链表,我想要一个名为startPointer"的类型为 Item(Item 是类名)的静态指针,以便当我调用我的静态成员函数free"时,它可以通过使用 Item::startPointer 释放内存,但我收到一个错误(在代码后显示).请帮助,

                I am creating a linked list with self referential class in C++ and I want to have a static pointer of the type Item (Item is the class name) named "startPointer" so that when i call my static member function "free" , it can free up the memory by using Item::startPointer but i am getting an error(shown after code). Pls Help,

                class Item
                {
                    public:
                    std::string name;
                    int row,column;
                    int fileType;
                    Item *ptr;
                    static Item *startPointer;
                    void setNextPointer(Item* ptr)
                    {
                        ptr=ptr;
                    }
                    Item *getNextPointer()
                    {
                        return ptr;
                    }
                    static void free()
                        {
                        Item *p,*temp;
                        p=startPointer;
                        while(p!=NULL)
                        {
                            temp=p;
                            p=p->getNextPointer();
                            delete temp;
                        }
                    }
                
                };
                

                <小时>

                cube.o:cube.cpp:(.text$_ZN4Item4freeEv[Item::free()]+0x8): undefined reference to `Item::startPointer'
                collect2: ld returned 1 exit status
                
                mingw32-make.exe: *** [cube.exe] Error 1
                
                Execution terminated
                

                推荐答案

                你必须像这样定义你的静态成员:

                You have to define your static member like this:

                Item* Item::startPointer = nullptr; // or = NULL; if your cpp version is below c++11
                

                在单个编译单元(cpp文件)中写这样一行,否则只是声明成员.

                Write such a line in a single compilation unit (cpp file), otherwise the member is just declared.

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

                上一篇:可以在 C++ 中将类声明为静态吗? 下一篇:C++“致命错误LNK1120"未解析的静态类成员

                相关文章

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

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

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

                    <legend id='kCwOL'><style id='kCwOL'><dir id='kCwOL'><q id='kCwOL'></q></dir></style></legend>