静态指针数据成员 [英] static pointer data member

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

问题描述

编译此代码时

 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不知道为什么这个错误以及如何避免它们?????????



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

解决方案

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

<预LANG =C ++> 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。


我认为错误是您需要在全局范围内为相关静态指针提供实现:



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



(或当然也可以使用nullptr代替NULL)

<。 br />
像SA说的那样,不清楚为什么你需要这些是静态的......


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??????????????????????????????????????????????

解决方案

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;





Regards,
Ian.


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...


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆