我有一个使用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