我有一個使用std :: vector實現的n-way樹。 Node類僅保存指向根元素的指針,Link類具有指向其自身的std ::向量。當我創建它們時,我命名了每個鏈接,但是當我嘗試使用鏈接的名稱在此樹中找到一個節點,但卻出現了分段錯誤時。此外,我意識到我的函數GetLink()不做任何錯誤檢查,我嘗試了幾件事情,但他們沒有工作,所以如果可能的話,如何在這種情況下實施的任何建議將不勝感激。這裏是我的代碼:如何在一個n-way樹中找到一個孩子?
// in node.h
class Node {
public:
// constructors
// fuctions
private:
Link *root;
};
// in link.h
class Link {
public:
//EDIT: added vector initialization in the constructor
Link() : my_links(0) { }
// some functions
// EDIT: example of making the tree
bool Load(token) {
// parsing code based on token
else if (strcmp (temp, "link") == 0)
{
Link* lnk = new Link();
lnk->Load (token);
lnk->Init();
AddChild (lnk);
lnk->m_parent = this;
}
// some more parsing code
}
void Link::AddChild (Link* pChild)
{
my_links.push_back(pChild);
}
Link* Link::GetLink(char* str) // this is the function that is the problem.
{
if (strcmp(name, str) == 0)
{
return this;
}
for (int i=0; i < (int) my_links.size(); i++)
{
//Edit: added check for NULL ptr
if (my_links[i] == NULL)
{
fprintf (stderr, "\n\t Couldn't find link\n\n");
break;
}
//Edit: typo corrected
return my_links[i]->GetLink(str);
}
}
private:
char name[256];
Link* m_parent;
std::vector<Link*> my_links;
};
// in main.cpp
static Node* node;
static Link* link;
main()
{
char *str = "link_3";
link = node->GetLink(str);
printf("\n found link: %s", link->GetName());
retrun 0;
}
編輯:重寫前面的代碼作爲MCVE
#include <cstdio>
#include <vector>
#include <cstring>
class Link {
public:
//EDIT: added vector initialization in the constructor
Link() : my_links(0)
{
m_parent = NULL;
}
void SetParent(Link* pParent)
{
m_parent = pParent;
}
// EDIT: example of making the tree
bool Load(char *str)
{
unsigned int len;
Link* lnk = new Link();
len = strlen(str);
strcpy(name, str);
lnk->SetParent(this);
AddChild (lnk);
return true;
}
void AddChild (Link* pChild)
{
my_links.push_back(pChild);
}
Link* GetLink(char* str) // this is the function that is the problem.
{
if (strcmp(name, str) == 0)
{
return this;
}
for (int i=0; i < (int) my_links.size(); i++)
{
//Edit: added check for NULL ptr
if (my_links[i] == NULL)
{
fprintf (stderr, "\n\t Couldn't find link\n\n");
break;
}
//Edit: typo corrected
return my_links[i]->GetLink(str);
}
fprintf(stderr, "\n\t Cannot get link\n\n");
return 0;
}
char* GetName()
{
return name;
}
private:
char name[256];
Link* m_parent;
std::vector<Link*> my_links;
};
class Node {
public:
Node()
{
root = NULL;
}
bool Load (char *str)
{
unsigned int len;
root = new Link(); // here is where the error occurs
len = strlen(str);
strcpy(name, str);
return true;
}
void AddChild (char *str)
{
root->Load(str);
}
Link* GetRoot()
{
return root;
}
private:
char name[256];
Link *root;
};
static Node* node;
static Link* lnk;
int main()
{
node->Load((char*)"I am root");
node->AddChild((char*)"I am child 1");
node->AddChild((char*)"I am child 2");
node->AddChild((char*)"I am child 3");
char *str = (char*)"I am child 2";
lnk = node->GetRoot()->GetLink(str);
printf("\n found link: %s", lnk->GetName());
return 0;
}
錯誤我現在得到在VS2010上77號線這是「根=新鏈接()」中的Node類,load()函數是:
Unhandled exception at 0x012e1bbe in nWayTree.exe: 0xC0000005: Access violation writing location 0x00000100.
如果您在使用'的std :: VECTOR',它不是你寫的,所以不要和C. – 2015-01-21 06:22:31
標記您的問題,您沒有初始化向量'的尺寸爲C '。可以'push_back'所有'Link'指針,或者在構造函數中初始化'vector'的大小。因此,'my_links.size()'給出了分段錯誤。 – shauryachats 2015-01-21 06:22:48
@ShauryaChats我正在初始化構造函數中的vector,並且有一個函數可以通過push_back添加子項,爲簡潔起見,我沒有在這裏包含它。 – Urler 2015-01-21 06:24:33