Will be initialized after warning fix

ⅰ亾dé卋堺 提交于 2021-02-04 21:30:21

问题


good evening (and happy thanksgiving),

I have the following code (ripped out of my main code into a standalone file) and am getting some warning messages that I'd like to resolve.

Here is the code:

#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;
}

when I do g++ test.cpp -Wall, I get the following warnings

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

Is there a way for me to rewrite/rearrange the code to keep it functional and remove these warnings? I am not familiar with this warning at all and have been reading up on it, but a lot of the code examples alone are hard for me to follow, let alone understanding the way to resolve it.

Any advice would be appreciated, thanks.


回答1:


Members are initialized in order of declaration. Having your member initialization list in some other order may be confusing to the programmer who might not be aware of which order is followed, or might not be aware that the members were declared in a different order and therefore might expect the order of member initialization to be the order of member initialization list - which it isn't in your case. The purpose of the warning is to highlight this fact. This fact may be very important in cases where initialization of one member depends on another.

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

Yes. By changing the order of member declarations to match the order of member initialization list i.e.

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

Alternatively, you could change the order of your member initialization list to match the order of declaration of the members.




回答2:


Variables are intialised in the order of their declaration, not in the order they appear in the initialiser list. Your compiler is simply warning you of this fact. You can just change the order and the warning will go away.




回答3:


Members are initialized in the order they are declared in the class. Regardless of the order you use in the initializer list. Initialize the members in the correct order and the warning will go away.



来源:https://stackoverflow.com/questions/47464164/will-be-initialized-after-warning-fix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!