警告修复后将初始化

Will be initialized after warning fix

本文关键字:初始化 警告      更新时间:2023-10-16

晚上好(感恩节快乐),

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

这是代码:

#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) {}

我有没有办法重写/重新编写代码以保持其功能并删除这些警告?我根本不熟悉此警告,并且一直在阅读它,但是仅凭我很难遵循许多代码示例,更不用说理解解决方法的方法了。

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

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

有没有办法重写/重新编写代码以保持其功能并删除这些警告?

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

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

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

变量是按声明的顺序进行的,而不是按照它们出现在初始化列表中的顺序。您的编译器只是警告您这一事实。您可以更改订单,警告将消失。

成员按在类中声明的顺序初始化。无论您在初始化列表中使用的顺序如何。以正确的顺序初始化成员,警告将消失。