0
下面是我使用的代码: gdb在启动构造函数后立即显示了分段错误。我可能会做错什么?如果我在另一个类中使用类的对象,我可以使用指向第一类对象的指针指向它的成员吗?
class Employee
{
public:
string name;
int height;
int level;
Employee* iboss;
Employee* parent;
Employee* left_child;
Employee* right_child;
vector<Employee*> junior;
};
class Company
{
private:
Employee* root;
Employee* rootAVL;
public:
Company(void)
{
root->junior[0]=NULL;
root->level=0;
root->iboss=NULL;
root->height=0;
root->left_child=NULL;
root->right_child=NULL;
root->parent=NULL;
rootAVL=root;
}
Employee* search(string A, Employee* guy,int c);
void AddEmployee(string A,string B);
void DeleteEmployee(string A,string B);
void LowestCommonBoss(string A,string B);
void PrintEmployees();
void insertAVL(Employee* compare,Employee* guy,int c);
void deleteAVL(Employee* guy);
void GetName(string A)
{
root->name=A;
cout<<A;
}
};
int main()
{
cout<<"hello world";
Company C;
//so the problem seems to be that there's a segmentation fault every time i try to access root or try to point to it's methods.
cout<<"hello world";
string f;
cin>>f;
C.GetName(f);
C.PrintEmployees();
}
这给了我,每当我尝试使用root->junior[0]=NULL
或诸如此类的事分割错误。
可能是什么问题?
提示:当'Company()'构造函数开始执行时'root'指针指向哪里? – Angew 2014-09-12 10:28:30
根点没有特别的地方,所以当你用它去除它会发生什么 - > – ldgorman 2014-09-12 10:29:54
我会建议阅读更多的指针 – ldgorman 2014-09-12 10:30:46