将在警告修复后初始化

Will be initialized after warning fix

晚上好(感恩节快乐),

我有以下代码(从我的主要代码中提取到一个独立文件中)并且收到了一些我想要解决的警告消息。

代码如下:

#include <cassert>
#include <ostream>
#include <climits>
#include <iostream>
#include <string>

using namespace std;

class WordCount
{
public:
   // Constructors
   WordCount() : word(""), count(0) { }
   WordCount(string theWord, unsigned theCount = 1) : word(theWord), count(theCount) { }

   // Accessors
   string Word() const { return word; }
   unsigned Count() const { return count; }

   // Mutator
   void Update() { ++count; }

   // Output a Word to a stream.
   void Show(ostream &os)  { os << word << "=" << count; }

   // Overloaded relational operators
    bool operator<(WordCount &rhs) { return word < rhs.word; }
    bool operator>(WordCount &rhs) { return word > rhs.word; }
    bool operator==(WordCount &rhs) { return word == rhs.word; }
    bool operator<=(WordCount &rhs) { return word <= rhs.word; }
    bool operator>=(WordCount &rhs) { return word >= rhs.word; }
    bool operator!=(WordCount &rhs) { return word != rhs.word; }

private:
   string   word;    // The word to be counted
   unsigned count;   // The number of occurrences
};

class Queue 
{
private:
    struct Node
    {
        WordCount data; //data in node
        Node *next;     //pointer to next node

        // Default Constructor
        Node() : next(0) {}
        // Explicit Constructor
        Node(const WordCount &theData, Node *const theNext = 0)
            : data(theData), next(theNext) { }
    };
public: 
    Queue() : head(0), tail(0) {}               
    bool Empty() const { return head == 0; }    
    void Enqueue(const WordCount &elem);            
    WordCount Dequeue();                            
    WordCount Head() { return head->data; }
private:
    Node *tail;     // "end" of queue
    Node *head;
};

void Queue::Enqueue(const WordCount &elem)
{
    Node* temp = new(nothrow) Node(elem);
    assert(temp != NULL);

    // head == tail if head == NULL, so must also be assigned temp
    if (head == NULL)
        head = temp;
    // add temp after current tail
    else
        tail->next = temp;
    // update tail adress to be new temp node
    tail = temp;
}

WordCount Queue::Dequeue()
{
    assert (!Empty());
    WordCount poppedData = head->data;
    Node *temp = head;
    head = head->next;
    if (head == NULL) 
        tail = NULL;
    delete temp;   
    return poppedData;
}

int main()
{
    return 0;
}

当我执行 g++ test.cpp -Wall 时,我收到以下警告

test.cpp: In constructor 'Queue::Queue()':
test.cpp:61:8: warning: 'Queue::head' will be initialized after [-Wreorder]
  Node *head;
        ^
test.cpp:60:8: warning:   'Queue::Node* Queue::tail' [-Wreorder]
  Node *tail;  // "end" of queue
        ^
test.cpp:54:2: warning:   when initialized here [-Wreorder]
  Queue() : head(0), tail(0) {}

有没有办法让我 rewrite/rearrange 保持代码正常运行并删除这些警告?我对这个警告一点都不熟悉并且一直在阅读它,但是很多代码示例本身就很难理解,更不用说理解解决它的方法了。

如有任何建议,我们将不胜感激。

变量是按照它们声明的顺序初始化的,而不是按照它们在初始化列表中出现的顺序。您的编译器只是警告您这个事实。您只需更改顺序,警告就会消失。

成员按声明顺序初始化。以其他顺序排列成员初始化列表可能会使程序员感到困惑,他们可能不知道遵循哪个顺序,或者可能不知道成员是以不同的顺序声明的,因此可能期望成员初始化的顺序是成员初始化列表的顺序 - 这不是你的情况。警告的目的是强调这一事实。在一个成员的初始化依赖于另一个成员的情况下,这一事实可能非常重要。

Is there a way for me to rewrite/rearrange the code to keep it functional and remove these warnings?

是的。通过更改成员声明的顺序以匹配成员初始化列表的顺序,即

Node *head;
Node *tail;     // "end" of queue

或者,您可以更改成员初始化列表的顺序以匹配成员声明的顺序。

成员按照它们在 class 中声明的顺序进行初始化。无论您在初始化程序列表中使用的顺序如何。以正确的顺序初始化成员,警告就会消失。