警告修复后将初始化
Will be initialized after warning fix
晚上好(感恩节快乐),
我有以下代码(从我的主代码中删除到独立文件中),并且正在收到一些我想解决的警告消息。
这是代码:
#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
另外,您可以更改会员初始化列表的顺序以匹配成员声明的顺序。
变量是按声明的顺序进行的,而不是按照它们出现在初始化列表中的顺序。您的编译器只是警告您这一事实。您可以更改订单,警告将消失。
成员按在类中声明的顺序初始化。无论您在初始化列表中使用的顺序如何。以正确的顺序初始化成员,警告将消失。
相关文章:
- g++ 在某个类成员未初始化时不发出警告
- 为什么 gcc 会给我可能未初始化的警告 deque::insert 带有过滤范围
- 在 Cocos2dx 中初始化粒子效果后的警告 - "libpng warning: iCCP: known incorrect sRGB profile"
- 在 C++11 中轻松初始化模板类的静态成员,没有 clang 警告
- C++ 警告:在一个声明中将截断从双精度初始化为浮点数,但在另一个声明中则不初始化
- 我可以在初始化之前使用 std::array 成员变量中的 data() 指针吗?发出警告
- 为什么 gcc 警告只针对统一初始化缩小转换范围?
- 有没有办法在初始化字符串时避免来自 clang-tidy(fuchsia-default-arguments)的警告?
- 未显式初始化C++全局变量时发出警告?
- 警告:变量未初始化
- 警告 C4267"正在初始化":从'size_t'转换为"DWORD",可能会丢失数据
- 编译时收到未初始化的内存警告
- 如果变量未初始化,Visual Studio 2017 不会生成任何警告
- 警告:'dymmy' 在此功能中可能未初始化使用
- 非初始化警告需求-O2
- 警告在结构内缺少初始化器
- C4838 警告,包含常量字符* 数组的数组初始化
- 警告C26495函数调用从构造函数到初始化变量,原因
- 如何禁用Wmaybe未初始化警告
- 禁用局部变量的未初始化警告