我遇到了這個實現的一系列錯誤。使用結構的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