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.