我遇到了这个实现的一系列错误。使用结构的C函数,为什么它不起作用?
typedef struct EmployeeStruct
{
char lastName[MAX_LENGTH];
char firstName[MAX_LENGTH];
int employeeNumber; // Holds the employee's ID. This value is
// equal to the number of employees
struct EmployeeStruct *Next; // Pointer to the next most recently hired Employee
}Employee;
当试图创建一个将返回指向此结构的指针的函数时,问题就出现了。这个错误出现在malloc调用中,导致“new”没有被正确声明,因此这个函数中的所有行都有错误。
Employee* hireEmployee(Employee tail, char lastName[MAX_LENGTH], char firstName[MAX_LENGTH])
{
struct Employee *new = (Employee*)malloc(sizeof(Employee));
new.lastName = lastName;
new.firstName = firstName;
new.next = tail;
tail.next = new;
new.employeeNumber = employeeCount;
return tail;
}
这是一个错误列表。谢谢您的帮助!
lab6.c:19: warning: initialization from incompatible pointer type
lab6.c:20: error: request for member ‘lastName’ in something not a structure or union
lab6.c:21: error: request for member ‘firstName’ in something not a structure or union
lab6.c:22: error: request for member ‘next’ in something not a structure or union
lab6.c:23: error: ‘Employee’ has no member named ‘next’
lab6.c:24: error: request for member ‘employeeNumber’ in something not a structure or union
lab6.c:26: error: incompatible types in return
即使你用C编写,使用一些流行的C++关键字,比如'new'也有点混乱。 – ouah
啊,真的。甚至没有想到这一点。好点谢谢 – AKon
你正在使用'next',但是定义了'Next'。你不声明'employeeCount'。你不是取消引用你的指针:使用'new-> Next'代替'new.next'。对于初学者。哦 - 你需要返回地址(因为你的函数的类型是Employee *'),而不是结构本身。 '返回&tail;' – Floris