I'm making something that needs to work with a queue, enqueue and dequeue. I made a simplified program(So i wont have to work with 12 classes) and it seems im getting the same problem. I have a class that holds an int and a pointer to it, and while im doing the enqueue dequeue rotations i lose the pointers value and i cant solve this. (I also need to make the queue myself so i cant use any libraries) I have the class 'son.cpp':
我正在做一些需要使用队列,入队和出队的东西。我做了一个简化的程序(所以我不必使用12个类),它似乎我得到了同样的问题。我有一个类,它持有一个int和一个指向它的指针,而我正在进行入队出队轮换,我失去了指针值,我无法解决这个问题。 (我还需要自己编队,所以我不能使用任何库)我有类'son.cpp':
son::son() {
random = 0;
}
void son::set_point() {
point = &random;
}
son::son(son &other) {
this->next = other.next;
this->prev = other.prev;
this->random = other.random;
}
void son::print_num() {
std::cout << "Random number is: " << random << std::endl;
}
son.h:
class son {
public:
int random;
son* next;
son* prev;
int* point;
son(son &other);
son();
void set_point();
void print_num();
~son();
};
sonqueue.h:
class sonqueue{
son *_head, *_tail;
public:
sonqueue();
bool isEmpty();
son* dequeue();
void enqueue(son&);
~sonqueue();
};
sonqueue.cpp:
sonqueue::sonqueue(){
_tail = NULL;
_head = NULL;
}
bool sonqueue::isEmpty() {
if (_tail == NULL && _head == NULL)
return true;
return false;
}
son* sonqueue::dequeue() {
if (!isEmpty()) {
std::cerr <<"in queues head random "<< (_tail->point)<<std::endl;
son *sp = new son(*_head);
son *temp = sp->prev;
delete _head;
_head = temp;
_head->next = NULL;
return sp;
}
else {
exit(5);
}
}
void sonqueue::enqueue(son &ptr) {
if (!isEmpty()) {
_tail->prev = &ptr;
(ptr.next) = _tail;
_tail = &ptr;
ptr.prev = NULL;
}
else {
_tail = &ptr;
_head = &ptr;
ptr.prev = NULL;
ptr.next = NULL;
}
}
sonqueue::~sonqueue(){
son *point = _tail;
while (point->next != NULL) {
son *to_be_del = dequeue();
delete to_be_del;
}
}
and the main.cpp:
和main.cpp:
int main() {
sonqueue *sq = new sonqueue();
for (int i = 0; i < 3; i++) {
son *son_to_be = new son();
son_to_be->random = i;
son_to_be->set_point();
sq->enqueue(*son_to_be);
}
while (std::cin.get() != (char)27) {
son *son_obj = sq->dequeue();
son_obj->print_num();
sq->enqueue(*son_obj);
}
system("PAUSE");
}
I must be missing something because this code works perfectly with ints and such but when i try to do this with pointers i lose that value(As the address changes) How can i implement this so it also carries the pointers value?
我必须遗漏一些东西,因为这段代码完全适用于int等等但是当我尝试使用指针执行此操作时我会丢失该值(因为地址更改)我如何实现它以便它还带有指针值?
EDIT: What i mean is when i try to go over the queue(enqueue-dequeue-enqueue...) I lose the value of point as its not going together with the other data members of the object(I believe because a it pointing to an address it doesnt copy the address)
编辑:我的意思是当我试图越过队列(enqueue-dequeue-enqueue ...)我失去了点的价值,因为它不与对象的其他数据成员一起(我相信因为它指向到一个地址,它不复制地址)
Thanks
1 个解决方案
#1
0
I'm quite sure that it has to do with copying son
-objects within queue-operations. But as you already have a copy constructor in place, extending this with an adaption of point
should solve your problem:
我很确定它与在队列操作中复制子对象有关。但是,由于您已经有一个复制构造函数,通过适应点来扩展它应该可以解决您的问题:
son::son(son &other) {
this->next = other.next;
this->prev = other.prev;
this->random = other.random;
this->point = &this->random;
}
There might be other issues as well, e.g. memory leaks, but that's not your question, right?
可能还有其他问题,例如内存泄漏,但这不是你的问题,对吧?
#1
0
I'm quite sure that it has to do with copying son
-objects within queue-operations. But as you already have a copy constructor in place, extending this with an adaption of point
should solve your problem:
我很确定它与在队列操作中复制子对象有关。但是,由于您已经有一个复制构造函数,通过适应点来扩展它应该可以解决您的问题:
son::son(son &other) {
this->next = other.next;
this->prev = other.prev;
this->random = other.random;
this->point = &this->random;
}
There might be other issues as well, e.g. memory leaks, but that's not your question, right?
可能还有其他问题,例如内存泄漏,但这不是你的问题,对吧?