警告修复后将初始化

Tre*_*als 1 c++ warnings initialization class

晚上好(感恩节快乐),

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

这是代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

当我执行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) {}
Run Code Online (Sandbox Code Playgroud)

我有办法重写/重新排列代码以使其保持功能并消除这些警告吗?我完全不熟悉此警告,并且已经阅读了该警告,但是仅我自己很难遵循许多代码示例,更不用说理解解决方法。

任何建议,将不胜感激,谢谢。

eer*_*ika 5

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

我有办法重写/重新排列代码以使其保持功能并消除这些警告吗?

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

Node *head;
Node *tail;     // "end" of queue
Run Code Online (Sandbox Code Playgroud)

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