Click here to Skip to main content
15,795,331 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
As compiling this code
class Least_Recently_Used
	static int count;	// this variable holding the number of allocated blocks (valid blocks), holds a set size as max. value.
	static Least_Recently_Used *next;	// points to the next block in the cache
	static Least_Recently_Used *head;
	static Least_Recently_Used *tail;

	void LRU_retrive();
	void LRU_insert();
	void LRU_evict();
	int Least_Recently_Used::count = 0;
	Least_Recently_Used Least_Recently_Used::*next = NULL;	// points to the next block in the cache
	Least_Recently_Used Least_Recently_Used::*head = NULL;
	Least_Recently_Used Least_Recently_Used::*tail = NULL; 

 void Least_Recently_Used::LRU_evict()<br />
{<br />
	this->tail = --this->tail;<br />
	this->tail->next = NULL;<br />

i get this errors:

/tmp/ccWxYlaB.o: In function `Least_Recently_Used::LRU_evict()':
Cache.cpp:(.text+0xa5): undefined reference to `Least_Recently_Used::tail'
Cache.cpp:(.text+0xad): undefined reference to `Least_Recently_Used::next'

i don't know why this errors and how to avoid them??????????

please can anyone help me??????????????????????????????????????????????
Updated 26-Apr-13 15:32pm
Sergey Alexandrovich Kryukov 26-Apr-13 22:23pm    
Why, why static? Does not seem to make any sense... Or, can you explain?

Where you have implemented the static pointers, you have placed the asterisk in the wrong place; it needs to be before the class name part of the declaration. Actually, this is one example where placing the asterisk next to the type name, instead of putting it with the variable, would help not make these sorts of mistakes:
Least_Recently_Used* Least_Recently_Used::next = NULL;  // points to the next block in the cache
Least_Recently_Used* Least_Recently_Used::head = NULL;
Least_Recently_Used* Least_Recently_Used::tail = NULL;

not, as you have it, after the double colon (::*).

That will solve the problem.

However, since you have declared these static, I think you should not use the "this->" pointer to access them. (The C++ compiler doesn't seem to mind, but it seems a somewhat odd thing to do).

You should seriously consider whether you mean these variables to be static, meaning that there is only one instance of them used by all objects of this type. Or should they rather be instance members of the object.
If you really do need them to be static, for whatever reason, you need to consider thread safety, and you need to consider what is managing the memory they point to - when does it get allocated, and perhaps more importantly, when will it be released? Etc, etc.

On further examination, "next" should definitely not be static! Presumably this is some sort of linked-list, with a "head" and a "tail". "next", it would seem, should point to the next member in the list, but by having is static you only have one "next" value for the whole lot, so your linked list is broken.

Furthermore, when you do this:
this->tail = --this->tail;
have you got any idea (or can you be sure) what "tail" will be pointing to? Do you rather need to turn your "Least_Recently_Used" class into a double-linked list item - by adding a "prev" ("previous") member, so that you could do something like this instead:
tail = tail->prev;

Share this answer
Ian A Davidson 27-Apr-13 0:39am    
Apologies for the double post. I'm not sure why that happened - I only clicked "submit" once!
nv3 27-Apr-13 3:35am    
Good points, Ian, and thorough analysis. My 5.
Stefan_Lang 29-Apr-13 6:41am    
Actually, this code:
this->tail = --this->tail;
Does nothing at all: the RHS is the same static variable that is on the LHS. Lets just ignore that --this is complete and utter noonsense - in this context it (thankfully) has no effect.
Ian A Davidson 29-Apr-13 9:26am    
That is well spotted that the decrement will already have been applied! Well done.

"--x" is the same as "x = x-1", so in this case "--this->tail" is the same as "this->tail = this->tail - 1", so the code is doing "this->tail = (this->tail = this->tail -1)" where the ending result is the same, but the assignment is being done twice, as if he'd written "this->tail = this->tail - 1; this->tail = this->tail". And this effect is the same whether the variable is static or not!
(-- is being applied to "tail", not "this", because operator precendance dictates that -> operator will be evaluated before the prefix -- operator)

The "this" has no place (is utter nonsense, as you put it) because the variable is static. So "this->tail = --this->tail", if it were to be kept should be replaced with a simple "--tail".

However, the point I made regarding this remains, in that I don't believe he would have any idea what "tail" would end up pointing to. It's obviously meant to be a linked list, having a "next" pointer, in which case it obviously cannot be guaranteed that the "previous" pointer is immediately before "tail", otherwise what's the point of "next".

I think the error is that you need to provide an implementation at global scope for the static pointers in question:

// [in .cpp file]
Least_Recently_Used *Least_Recently_Used::head = NULL;
Least_Recently_Used *Least_Recently_Used::tail = NULL;

(or of course you can use nullptr instead of NULL).

Like SA says, it is not clear why you need these to be static...
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900