[英]Segmentation fault (core dumped) on Ubuntu with matrix functions on C++
[英]Segmentation fault(core dumped) with C++ on ubuntu
我實現了一棵紅黑樹,但是當我從終端運行程序時,它給出了錯誤:分段故障核心已轉儲。 我認為我正在訪問一個本不應該訪問的位置,或者正在訪問空值。
這是我的插入方法:
template <class Item, class Key>
void RedBlackTree<Item, Key>::Insert(RedBlackTree<Item,Key> Tree, Node<Item, Key> *z)
{
Node <Item, Key> *T=0;
Node<Item, Key>* nill=T->nill;
Node<Item, Key>* root=T->root;
Node<Item, Key> *y;
Node<Item, Key> *x;
y=nill;
x=root;
//x= T->getRoot();
while(x != nill)
{
y=x;
if(z->getKey() < x->getKey())
x= x->getLeft();
else
x = x->getRight();
}
z->setParent(y);
if(y == nill)
z=T->root;
else
if((z->getKey())<(y->getKey()))
{
y->setLeft(z);
}
else
{
y->setRight(z);
}
z->setLeft(nill);
z->setRight(nill);
z->colour1 = 'R';
FixingInsert(Tree,z);
}
這是我主要的一部分:
Node<int, int> q = Node<int, int>(0,5);
RedBlackTree<int, int> tree1;
tree1.Insert(tree1, &q);
有人可以幫忙嗎? 我是模板的新手,如果有人幫助我,我將不勝感激。
提前致謝。
您正在取消引用NULL指針(兩次!):
Node <Item, Key> *T=0;
Node<Item, Key>* nill=T->nill;
Node<Item, Key>* root=T->root;
我有種想寫的感覺:
RedBlackTree <Item, Key> *T= &Tree;
Node<Item, Key>* nill=T->nill;
Node<Item, Key>* root=T->root;
要么
RedBlackTree <Item, Key> *T= this;
Node<Item, Key>* nill=T->nill;
Node<Item, Key>* root=T->root;
雖然這兩個是多余的,因為你已經在指針this
(但你可以直接訪問反正成員變量),你已經有對象Tree
,其成員就可以通過訪問Tree.
也一樣(只要它們是public
)。
另外,為什么還要將對象的副本傳遞給自身的方法呢? 這完全沒有必要。 即使參考也將是多余的。
您可能應該刪除Tree
參數,刪除行Node <Item, Key> *T=0;
並刪除所有T->
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.