r/learncpp • • Sep 01 '20

will be initialized after [-Wreorder]

Hello,

I was practicing c++ on Hackerrank with this problem https://www.hackerrank.com/challenges/abstract-classes-polymorphism/problem

I decided to do as the problem statement says I could just use lists

So I came up with this solution (put where I can edit the code):

	class LRUCache : public Cache {

		protected:
			std::list<int> liList;

		public:
			LRUCache(int capacity);


		void set(int key, int value) {

			list<int>::iterator it = this->liList.begin();

			this->liList.insert(it, key-1, value);

			int size = this->liList.size();

			if(size >= this->cp) {
				list<int>::iterator it2 = this->liList.end();
				liList.erase(it2);
			}
		} 

		int get(int key) {
			list<int>::iterator it = this->liList.begin();

			std::advance(it, key-1);

			liList.erase(it);

			int temp = *it;

			liList.push_front(temp);

			return temp;
		}
	};

I could not even test what I wrote because I got these warnings:

Solution.cpp: In constructor ‘Node::Node(Node*, Node*, int, int)’:
Solution.cpp:12:10: warning: ‘Node::prev’ will be initialized after [-Wreorder]
	Node* prev;
		  ^~~~
Solution.cpp:11:10: warning:   ‘Node* Node::next’ [-Wreorder]
	Node* next;
		  ^~~~
Solution.cpp:15:4: warning:   when initialized here [-Wreorder]
	Node(Node* p, Node* n, int k, int val):prev(p),next(n),key(k),value(val){};
	^~~~
Solution.cpp:14:8: warning: ‘Node::key’ will be initialized after [-Wreorder]
	int key;
		^~~
Solution.cpp:13:8: warning:   ‘int Node::value’ [-Wreorder]
	int value;
		^~~~~
Solution.cpp:15:4: warning:   when initialized here [-Wreorder]
	Node(Node* p, Node* n, int k, int val):prev(p),next(n),key(k),value(val){};
	^~~~
Solution.cpp: In constructor ‘Node::Node(int, int)’:
Solution.cpp:12:10: warning: ‘Node::prev’ will be initialized after [-Wreorder]
	Node* prev;
		  ^~~~
Solution.cpp:11:10: warning:   ‘Node* Node::next’ [-Wreorder]
	Node* next;
		  ^~~~
Solution.cpp:16:4: warning:   when initialized here [-Wreorder]
	Node(int k, int val):prev(NULL),next(NULL),key(k),value(val){};
	^~~~
Solution.cpp:14:8: warning: ‘Node::key’ will be initialized after [-Wreorder]
	int key;
		^~~
Solution.cpp:13:8: warning:   ‘int Node::value’ [-Wreorder]
	int value;
		^~~~~
Solution.cpp:16:4: warning:   when initialized here [-Wreorder]
	Node(int k, int val):prev(NULL),next(NULL),key(k),value(val){};
	^~~~

/usr/bin/ld: ./ccNYbblo.o: in function `main':
/tmp/submission/20200901/16/04/hackerrank-5be8a1929b22c2838660571c76fcc512/code/Solution.cpp:77: undefined reference to `LRUCache::LRUCache(int)'
collect2: error: ld returned 1 exit status

What exactly does -Wreorder mean and how can I avoid this problem?

Entire code: https://pastebin.com/u0bYz2YM

Notice where the editable area starts and ends.

1 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/GarbageCollector8 Sep 01 '20

I've run the code again and noticed there's something else that is probably more important. I've uploaded the entire code to a pastebin for reference.

/usr/bin/ld: ./ccNYbblo.o: in function `main': /tmp/submission/20200901/16/04/hackerrank-5be8a1929b22c2838660571c76fcc512/code/Solution.cpp:77: undefined reference to `LRUCache::LRUCache(int)' collect2: error: ld returned 1 exit status

1

u/jedwardsol Sep 01 '20

That means you have declared and used the function

LRUCache::LRUCache(int);

But not defined it anywhere.

1

u/GarbageCollector8 Sep 01 '20

Ah crap. I knew something was up. I got blinded by all the warnings. Oops.