本文介绍了静态指针数据成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译此代码时

 class Least_Recently_Used 
{
protected:
static int count; //此变量保存已分配块的数量(有效块),将设置大小保持为最大值。值。
静态Least_Recently_Used * next; //指向缓存中的下一个块
static Least_Recently_Used * head;
static Least_Recently_Used * tail;

public:
Least_Recently_Used(int);
~Least_Recently_Used();
void LRU_retrive();
void LRU_insert();
void LRU_evict();
};
int Least_Recently_Used :: count = 0;
Least_Recently_Used Least_Recently_Used :: * next = NULL; //指向缓存中的下一个块
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得到此错误:



/tmp/ccWxYlaB.o:在功能`Least_Recently_Used :: LRU_evict() '':

Cache.cpp :(文本+ 0xA5的):未定义参考`Least_Recently_Used ::尾 ''

Cache.cpp :(。text + 0xad):未定义引用`Least_Recently_Used :: next''



i不知道为什么这个错误以及如何避免它们?????????



请任何人帮助我????????????

解决方案
如果已经实现了静态指针,则将星号放在错误的位置;它需要在声明的类名部分之前。其实,这是一个例子,其中放置了星号的类型名称旁边,而不是把它与变量,将有助于不会让这几类错误:

Least_Recently_Used * Least_Recently_Used :: next = NULL; // 指向缓存中的下一个块
Least_Recently_Used * Least_Recently_Used :: head = NULL;
Least_Recently_Used * Least_Recently_Used ::尾= NULL;



不是如你有它,双冒号后(:: * )。



这将解决问题。



但是,既然你已宣布这些静态,我认为你不应该使用this->指针访问它们。 (C ++编译器似乎并不介意,但这似乎有点奇怪)。



您应该认真考虑是否意味着这些变量是static,意味着此类型的所有对象只使用它们的一个实例。或者他们应该是对象的实例成员。

如果你确实需要它们是静态的,无论出于何种原因,你需要考虑线程安全,你需要考虑管理内存的是什么他们指出 - 它什么时候被分配,或许更重要的是,什么时候会被释放?等等



进一步检查,下一步绝对不应该是静态的!据推测,这是某种链接列表,具有头部和尾部。看起来,next应该指向列表中的下一个成员,但是如果是静态的,那么整个批次只有一个下一个值,因此链接列表会被破坏。



此外,执行此操作时:

   - >尾部=  -    - >尾部; 

你有什么想法(或者你能确定)尾巴会指向什么?您是否需要将Least_Recently_Used类转换为双链接列表项 - 通过添加prev(previous)成员,以便您可以执行以下操作:

 tail = tail-> prev; 









问候,

Ian。



As compiling this code

class Least_Recently_Used
{
protected:
	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;

public:
	Least_Recently_Used(int);
	~Least_Recently_Used();
	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??????????????????????????????????????????????

解决方案



这篇关于静态指针数据成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 21:28
查看更多