Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如果我在另一个类中使用一个类的对象,我可以使用指向第一个类的对象的指针来指向它吗';s的成员?_C++_Class_Pointers - Fatal编程技术网

C++ 如果我在另一个类中使用一个类的对象,我可以使用指向第一个类的对象的指针来指向它吗';s的成员?

C++ 如果我在另一个类中使用一个类的对象,我可以使用指向第一个类的对象的指针来指向它吗';s的成员?,c++,class,pointers,C++,Class,Pointers,以下是我使用的代码: gdb在启动构造函数后立即显示分段错误。我可能做错了什么 class Employee { public: string name; int height; int level; Employee* iboss; Employee* parent; Employee* left_child; Employee* right_child; vector<Employee*> junior; }; c

以下是我使用的代码: 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();
}
class员工
{
公众:
字符串名;
内部高度;
智力水平;
雇员*iboss;
雇员*父母;
雇员*左子女;
雇员*右子女;
初级病媒;
};
阶级公司
{
私人:
员工*root;
员工*rootAVL;
公众:
公司(无效)
{
root->junior[0]=NULL;
根->级别=0;
root->iboss=NULL;
根->高度=0;
root->left\u child=NULL;
root->right\u child=NULL;
root->parent=NULL;
rootAVL=根;
}
雇员*搜索(字符串A,雇员*盖伊,整数c);
无效的添加员工(字符串A、字符串B);
作废删除员工(字符串A、字符串B);
void lowercommonboss(字符串A、字符串B);
无效打印员工();
void insertAVL(雇员*比较,雇员*盖伊,整数c);
void deleteAVL(员工*盖);
void GetName(字符串A)
{
根->名称=A;

在课堂上你有:

公司
建造商中,您需要:

但是您没有构造
Employee
的任何实例,因此
root
指针无效。因此,您只是尝试使用前面提到的
root->junior…
行访问无效内存

考虑先创建一名
员工

还请注意,如果您执行
root->junior[0]=…
,则还应将
员工的
std::vector junior
数据成员创建为至少包含一个项的向量(您尝试访问的索引为0的项)


最后,考虑使用<代码> NulLPTR <代码>在C++ 11/14代码中使用<代码> null <代码> .< /p>提示:当<代码>公司时,<代码>根>代码>指针指向哪里?
constructor开始执行?根点不在特定的位置,所以当您使用->解除保护它时会发生什么情况?我建议您多阅读一点指针
Employee* root;
root->junior[0]=NULL;