C++ 在单链表中插入值时出错 (E0137)

C++ Error inserting values in Singly Linked List (E0137)

我创建了一个单链表,它是 运行 完美的,直到我从单个 更改了我的 "basic" 数据结构int 值到各种变量。

错误发生在 insertAtEnd 函数中,当我尝试存储它从临时节点上的参数获取的数据时。

temp->nome = n;
temp->morada = m;
temp->telefone = t;
temp->idade = i;

int 值显示没有错误,但是 char[] 值显示错误 "E0137 - expression must be a modifiable lvalue".

List.h

struct node
{
    char nome[20];
    char morada[30];
    char telefone[9];
    Int idade;
    node *next;
};

class LinkedList
{
private:
    node *head;
    node *tail;
public:
    LinkedList();
    ~LinkedList();
    void insertAtEnd(char n[20], char m[30], char t[9], int i);
    void insertAtStart(char n[20], char m[30], char t[9], int i);
    void display(void);
    void deleteFirst(void);
    void deleteLast(void);
    void deleteAtPosition(int pos);
};

List.cpp

(...)
LinkedList::LinkedList()
{
    head = NULL;
    tail = NULL;
}
void LinkedList::insertAtEnd(char n[20], char m[30], char t[9], int i)
{
    node *temp = new node;
    temp->nome = n;
    temp->morada = m;
    temp->telefone = t;
    temp->idade = i;
    temp->next = NULL;
    if (head == NULL)
    {
        head = temp;
        tail = temp;
        temp = NULL;
    }
    else
    {
        tail->next = temp;
        tail = temp;
    }
}
(...)

Main.cpp

(...)
LinkedList lista;
    char nome[20];
    char morada[30];
    char telefone[9];
    int idade;
    (...)
    switch (op) {
        case 1:
            cout << "Inserir nome: ";
            cin >> nome;
            cout << "Inserir morada: ";
            cin >> morada;
            cout << "Inserir telefone: ";
            cin >> telefone;
            cout << "Inserir idade: ";
            cin >> idade;
            lista.insertAtEnd(nome, morada, telefone, idade);
            break;
(...)

这些是我认为相关的项目中的一些代码块,我几乎可以肯定上面没有任何其他内容与此问题有关,但如果您确实这么认为,请发表评论并我会编辑它。

在此先感谢。

您的代码生成以下错误:在成员函数“void LinkedList::insertAtEnd(char*, char*, char*, int)”中:错误:将“char*”赋值给“char”时类型不兼容[20]'。您必须将函数参数中的每个值复制到节点(我的意思是对数组中的每个值执行 temp->nome[0] = n[0])。您可以更改节点结构以使用指针而不是数组,这会变得更容易。 如果您想了解更多信息,请查看以下 link :

https://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm