30/09/2018, 20:42
Thắc mắc về lỗi stop working trong code Linked List
Chào mọi người, hiện tại em đang học lập trình C++. Tới bài linklist,viết code chạy thử thì ra lỗi stop working
Như vậy thì liệu code em có viết đúng không ạ? Làm sao để khắc phục tình trạng này?
Em đang dùng codeblock.
sourse code của em:
node.h
#ifndef NODE_H
#define NODE_H
class node
{
public:
node(void);
void setValue(int val);
void setNext(node*const next);
void setPrev(node*const prev);
int getValue()const;
node*getNext()const;
node*getPrev()const;
bool hasNext()const;
bool hasPrev()const;
virtual ~node(void);
protected:
private:
int value_;
node*next_;
node*prev_;
};
#endif // NODE_H
node.cpp
#include "node.h"
#include <iostream>
node::node(void)
{
this->value_=0;
this->next_=0;
this->prev_=0;
}
void node::setValue(int val)
{
this->value_=val;
}
void node::setNext(node*const next)
{
std::cout<<value_<<"->"<<next_->value_<<std::endl;
this->next_=next;
}
void node::setPrev(node*const prev)
{
std::cout<<prev_->value_<<"<-"<<value_<<std::endl;
this->prev_=prev;
}
int node::getValue()const
{
return this->value_;
}
node*node::getNext()const
{
if(this->hasNext())
{
return this->next_;
}
else
return 0;
}
node*node::getPrev()const
{
if(this->hasPrev())
{
return this->prev_;
}
else
return 0;
}
bool node::hasNext()const
{
return this->next_!=0;
}
bool node::hasPrev()const
{
return this->prev_!=0;
}
node::~node()
{
//dtor
}
main.cpp
#include <iostream>
#include "node.h"
using namespace std;
int main()
{
node*first=new node;
first->setValue(0);
node*currentnode=first;
for(int i=1;i<4;i++)
{
node*newnode=new node;
newnode->setValue(i);
currentnode->setNext(newnode);
newnode->setPrev(currentnode);
currentnode=newnode;
}
return 0;
}
Em cảm ơn ạ.
Bài liên quan
trường hợp này bạn nên xem lại source xem có bị lỗi phần nào không? hoặc post thông báo lỗi lên nhé
Khi em built thì không thấy báo lỗi ạ. Nhưng khi chạy thì ra stop working.
vậy thì xem lại phần code thuật toán đi, phần lớn crash là do code thuật tóan
Built không lỗi là không sai syntax. Nhưng thuật toán có vấn đề khi chạy mới sinh ra lỗi.
Ví dụ tràn mảng ; không kiểm soát được con trỏ để nó trỏ lung tung ; thao tác với con trỏ khi nó chưa khởi tạo hoặc đã bị xoá…
Em có đăng luôn code lên rồi ạ. Anh xem giúp em với. hic
Em có edit lại đăng kèm luôn source code rồi ạ. Mọi người xem giúp em với. Em cảm ơn ạ.
bỏ code vào markdown đi bạn
Hic bỏ thế nào vậy ạ?
http://daynhauhoc.com/t/cach-post-code-dung-markdown-trong-category-programming
Dạ em sửa lại rồi ạ. Anh xem giúp em nhé.
có thể là lỗi con trỏ tham chiếu sai bộ nhớ, trong Java lỗi này được gọi là NullReferenceException, ví dụ như danh sách có node1->node2->node3 mà bạn tham chiếu đến node4 thì nó báo lỗi.
Anh có thể nói chi tiết hơn không ạ? Em vẫn chưa hiểu lắm.
Ở đây em chỉ tạo ra 4 node thôi chứ đâu có làm gì thêm đâu ạ?
mình đã chạy code của bạn, lỗi là ở chỗ mấy hàm
std::cout
ấyAnh nói rõ hơn được không ạ? Em xem code mẫu thì giống hệt. Chỗ code mẫu cũng ghi là std::cout và vẫn chạy bình thường ạ.
A hiểu rồi, cảm ơn anh ạ.
bạn hiểu sao giải thích mình với, mình chỉ biết là ở đó có lỗi, comment đi thì hết lỗi @@
Ặc, thực ra chỉ cần đổi từ next_ thành next và prev_ thành prev trong phép in là được ạ. Nhưng tại sao thì em cũng không rõ lắm.