Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will be initialized after warning fix

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.

like image 201
Trever Wagenhals Avatar asked Sep 11 '25 15:09

Trever Wagenhals


1 Answers

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.

like image 112
eerorika Avatar answered Sep 14 '25 05:09

eerorika