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