链表访问私有成员错误C2248



我在这里创建了一个员工链接列表类,我的代码是:

Node.cpp

#include "EmpNode.h"
    EmpNode::EmpNode(int id, string name){
        emp.id = id;
        emp.name = name;
        next = NULL;
    }

List.cpp

#include "List.h"
#include "Header.h"
bool ListOfEmp::insertEmp(int id, string name){
    EmpNode *newNode = new EmpNode(id, name);
    if (!newNode){
        return false; // Failure
    }
    else{
        newNode->next = head;
        head = newNode;
        return true; // Success
    }
}
bool ListOfEmp::findEmp(int id, const Employee &emp) const{
    EmpNode *currentNode = head;
    while (currentNode != 0){
        if (currentNode->emp.id == id){
            emp = currentNode->emp;
            return true;
        }
        currentNode = currentNode->next;
    }
    return false;
}

节点.h

#pragma once
// My Node
class EmpNode {
    friend class ListOfEmp;
public:
    EmpNode(int id, string name);
private:
    Employee emp;
    EmpNode *next;
};

List.h

#pragma once
// My List of Nodes
class ListOfEmp {
public:
    ListOfEmp();
    ~ListOfEmp();
    bool findEmp(int id, const Employee &emp) const;
    bool insertEmp(int id, string name);
private:
    EmpNode *head;
};

错误1错误C2248:"Employee::id":无法访问类"Employer"中声明的私有成员\\22 1 LinkedList

我认为您想使用其构造函数初始化emp成员,并且您要查找的是

EmpNode::EmpNode(int id, string name) 
    : emp(id, name),
      next(NULL)
{
}

相关内容

  • 没有找到相关文章

最新更新